Python Functions
‫إعداد‬:
‫م‬.‫محمد‬ ‫علي‬
Objectives
 Functions in Python
 Create a function
 Create a function with arguments
 Pass more than one arguments
 Default Parameter values
 Create a function and return the value
 Recursion
Functions in Python
 A function is a block of code which only runs when it is called.
 We can pass data, known as parameters, into a function.
 A function can return data as a result
 We must define the function before using it
 We can create a function in python using (def) keyword
Create a function
 Example:
def example_function():
print(“welcome from function”)
example_function()
Create a function with arguments
 We can pass data, known as parameters, into a function.
 We can use this parameters in the function
 Example:
def example_function(arg):
print(“this is the argument you pass to the function”, arg)
example_function(“ali”)
Pass more than one arguments
 We can pass more than one parameters into the function
 We can use this parameters in the function
 Example:
def example_function(arg1,arg2):
print(“this is the argument you pass to the function”, arg)
print(“this is the argument you pass to the function”, arg)
example_function(“ali”,”mohammad”)
Pass more than one arguments
 Arbitrary Arguments, *args:
 If you do not know how many arguments that will be passed into your
function, add a (*) before the parameter name in the function definition.
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function(“Ali", “Mohammad", “Ghadeer")
The Result is: The youngest child is Ghadeer
Pass more than one arguments
 Keyword Arguments:
 You can also send arguments with the key = value syntax.
 This way the order of the arguments does not matter.
 Example:
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = “Ali", child2 = “Mohammad", child3 = “Ghadeer")
 Result: Ghadeer
Pass more than one arguments
 Arbitrary Keyword Arguments, **kwargs:
 If we do not know how many keyword arguments that will be passed into
your function, add two asterisk (**) before the parameter name in the
function definition.
 This way the function will receive a dictionary of arguments, and can
access the items accordingly.
 Example:
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = “Ali", lname = “Mohammad")
Default Parameter values
 The following example shows how to use a default parameter value.
 If we call the function without argument, it uses the default value:
 Example:
def my_function(city = “Homs"):
print("I am from " + city)
my_function(“Damascus")
my_function(“Tartous")
my_function()
my_function(“Hama")
Passing a List as an Argument
 You can send any data types of argument to a function (string, number,
list, dictionary etc.), and it will be treated as the same data type inside the
function.
 Example:
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
Create a function and return the value
 To let a function return a value, use the (return) keyword
 Example:
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Recursion
 Python also accepts function recursion, which means a defined function
can call itself.
 Recursion is a common mathematical and programming concept. It means
that a function calls itself. This has the benefit of meaning that you can
loop through data to reach a result.
 Example:
Recursion
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k - 1)
print(result)
else:
result = 0
return result
print("Recursion Example Results")
tri_recursion(6)
Result:
1
3
6
10
15
21

Viii session

  • 1.
  • 2.
    Objectives  Functions inPython  Create a function  Create a function with arguments  Pass more than one arguments  Default Parameter values  Create a function and return the value  Recursion
  • 3.
    Functions in Python A function is a block of code which only runs when it is called.  We can pass data, known as parameters, into a function.  A function can return data as a result  We must define the function before using it  We can create a function in python using (def) keyword
  • 4.
    Create a function Example: def example_function(): print(“welcome from function”) example_function()
  • 5.
    Create a functionwith arguments  We can pass data, known as parameters, into a function.  We can use this parameters in the function  Example: def example_function(arg): print(“this is the argument you pass to the function”, arg) example_function(“ali”)
  • 6.
    Pass more thanone arguments  We can pass more than one parameters into the function  We can use this parameters in the function  Example: def example_function(arg1,arg2): print(“this is the argument you pass to the function”, arg) print(“this is the argument you pass to the function”, arg) example_function(“ali”,”mohammad”)
  • 7.
    Pass more thanone arguments  Arbitrary Arguments, *args:  If you do not know how many arguments that will be passed into your function, add a (*) before the parameter name in the function definition. def my_function(*kids): print("The youngest child is " + kids[2]) my_function(“Ali", “Mohammad", “Ghadeer") The Result is: The youngest child is Ghadeer
  • 8.
    Pass more thanone arguments  Keyword Arguments:  You can also send arguments with the key = value syntax.  This way the order of the arguments does not matter.  Example: def my_function(child3, child2, child1): print("The youngest child is " + child3) my_function(child1 = “Ali", child2 = “Mohammad", child3 = “Ghadeer")  Result: Ghadeer
  • 9.
    Pass more thanone arguments  Arbitrary Keyword Arguments, **kwargs:  If we do not know how many keyword arguments that will be passed into your function, add two asterisk (**) before the parameter name in the function definition.  This way the function will receive a dictionary of arguments, and can access the items accordingly.  Example: def my_function(**kid): print("His last name is " + kid["lname"]) my_function(fname = “Ali", lname = “Mohammad")
  • 10.
    Default Parameter values The following example shows how to use a default parameter value.  If we call the function without argument, it uses the default value:  Example: def my_function(city = “Homs"): print("I am from " + city) my_function(“Damascus") my_function(“Tartous") my_function() my_function(“Hama")
  • 11.
    Passing a Listas an Argument  You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.  Example: def my_function(food): for x in food: print(x) fruits = ["apple", "banana", "cherry"] my_function(fruits)
  • 12.
    Create a functionand return the value  To let a function return a value, use the (return) keyword  Example: def my_function(x): return 5 * x print(my_function(3)) print(my_function(5)) print(my_function(9))
  • 13.
    Recursion  Python alsoaccepts function recursion, which means a defined function can call itself.  Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.  Example:
  • 14.
    Recursion def tri_recursion(k): if(k >0): result = k + tri_recursion(k - 1) print(result) else: result = 0 return result print("Recursion Example Results") tri_recursion(6) Result: 1 3 6 10 15 21