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.
def my_function(): print("Hello world")
To call a function in Python, simply declare the function name followed by parenthesis.
def my_function(): print("Hello world") my_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.
def my_function(message, num): print(message) print(num) my_function("Hello from the Python World!", 100)
A function can also return result. The return keyword is used to return value or result from a function.
def add_function(a, b): result = a + b return result result = add_function(100, 58) print(result)
In Python, you can also pass arguments as a key value pair.
def substract_function(a, b): result = a - b return result substract_function(a = 10, b = 5)
In Python, you can allow a function to accept any number of arguments by simply using double asterisk ** as a prefix of an argument.
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")
In Python, you can allow a function to accept any number of arguments by simply using single asterisk * as a prefix of an argument.
def my_function(*customer): firstname = customer lastname = customer print("Customer's Firstname is " + firstname + " and Lastname is " + lastname) my_function("Danny", "Cham")
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.
def my_function(a = 10): print("Default value of a is " + str(a)) my_function() my_function(20)
In Python, a function body cannot be empty. However, you can use the pass statement to prevent error.
def my_function(): pass
In Python, you can pass value of any type (Number, String, List, Dictionary) as an argument into a function.
def my_function(fruits_list): for i in fruits: print(i) fruits = ["apple", "banana", "grapes", "orange", "mango"] my_function(fruits)
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.
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))
Note: A developer must be careful when writing a recursive function as you may end up writing a function that never terminates.