Python Function
A function is a block of code that is referred to by its name and only runs when it is called.
To create a function in Python, the def keyword is used followed by function name and parenthesis.
Example
def my_function():
print("Hello world")
Calling a Function in Python
To call a function in Python, simply declare the function name followed by parenthesis.
Example
def my_function():
print("Hello world")
my_function()
Output
Passing Data into a Function
We can pass data into a function as parameters which are also called arguments. A function can have any number ko arguments and must be called with the correct number of arguments. If a function has two arguments then it must be called with two arguments not less or more.
Example
def my_function(message, num):
print(message)
print(num)
my_function("Hello from the Python World!", 100)
Output
100
Function Return Result
A function can also return result. The return keyword is used to return value or result from a function.
Example
def add_function(a, b):
result = a + b
return result
result = add_function(100, 58)
print(result)
Output
Function with Keyword Arguments
In Python, you can also pass arguments as a key value pair.
Example
def substract_function(a, b):
result = a - b
return result
substract_function(a = 10, b = 5)
Output
**kwargs - Arbitrary Keyword Arguments
In Python, you can allow a function to accept any number of arguments by simply using double asterisk ** as a prefix of an argument.
Example
def my_function(**customer):
firstname = customer["firstname"]
lastname = customer["lastname"]
print("Customer's Firstname is " + firstname + " and Lastname is " + lastname)
my_function(firstname = "Danny", lastname = "Cham")
Output
*args - Arbitrary Arguments
In Python, you can allow a function to accept any number of arguments by simply using single asterisk * as a prefix of an argument.
Example
def my_function(*customer):
firstname = customer[0]
lastname = customer[1]
print("Customer's Firstname is " + firstname + " and Lastname is " + lastname)
my_function("Danny", "Cham")
Output
Default Argument Value
In Python, you can allow a function to have a default value. This default value is used when no values are passed into the function.
Example
def my_function(a = 10):
print("Default value of a is " + str(a))
my_function()
my_function(20)
Output
Default value of a is 20
Pass Statement
In Python, a function body cannot be empty. However, you can use the pass statement to prevent error.
Example
def my_function():
pass
Passing a List into a Function as an Argument Value
In Python, you can pass value of any type (Number, String, List, Dictionary) as an argument into a function.
Example
def my_function(fruits_list):
for i in fruits:
print(i)
fruits = ["apple", "banana", "grapes", "orange", "mango"]
my_function(fruits)
Output
banana
grapes
orange
mango
Recursion in Python
Recursion in Python means a function calling itself. A function that calls itself is called a recursive function.
A recursive function can be useful in solving complex problems by breaking down the problems into smaller repetitive problems. Recursion involves iteration by calling the function itself. You can also use looping instead of recursive approach but the function may get tedious.
There are certain complex problems which makes the use of recursive functions better and very efficient compare to the loops.
Example
def recursive_factorial(n):
if n == 1:
print(n)
return 1
else:
print (n,'*', end=' ')
return n * recursive_factorial(n-1)
print(recursive_factorial(4))
Output
24
Note: A developer must be careful when writing a recursive function as you may end up writing a function that never terminates.