A function is a block of statements which executes only when it is called somewhere in the program. Function provides re-usability of same code for different inputs, hence saves time and resources. There are many in-built functions in Python and one of the common function is print(). A user can create their own function which is also termed as user-defined functions.
In Python, a function is defined using def keyword along with function's name followed by parenthesis containing function's parameter(s), if it has any.
After defining the function, it can be called anywhere in the program with it's name followed by parenthesis containing function's parameter(s), if it has any.
#Defining function def function_name(parameters): statements #Calling function function_name(parameters)
def MyFunction(): print("Welcome to Python Programming!.") MyFunction()
Welcome to Python Programming!.
A parameter (or also known as argument) is a variable which is used to pass information inside a function. In above example, the function does not has any parameter. But a user can create a function with single or multiple parameters. Value of a parameter can be further used by the function to achieve desired result.
def MyFunction(name): print("Welcome to Python Programming! " + name +".") MyFunction("John") MyFunction("Marry") MyFunction("Sam")
Welcome to Python Programming! John. Welcome to Python Programming! Marry. Welcome to Python Programming! Sam.
Default value can be assigned to a parameter at the time of creating function. When the function is called without parameter then it uses default value.
def MyFunction(name = "Dude"): print("Welcome to Python Programming! " + name +".") MyFunction() MyFunction("John")
Welcome to Python Programming! Dude. Welcome to Python Programming! John.
def MyFunction(num): for i in num: if (i % 2 == 0): print(i ," is an even number.") else: print(i ," is an odd number.") MyFunction(range(1,6))
1 is an odd number. 2 is an even number. 3 is an odd number. 4 is an even number. 5 is an odd number.
A function can be used to return values. Please see example below:
def MyFunction(num, multiplier = 2): x = num * multiplier return x print(MyFunction(5, 3)) print(MyFunction(10)) print(MyFunction(25, 4))
15 20 100
Parameter values can be passed in function with key = value syntax also. In this way, the order of parameter does not matter.
def MyFunction(num1, num2): print(num1/num2) MyFunction(num2 = 2, num1 = 10)
5.0
If there is no information about numbers of parameters that will be passed in the function, use * in front of parameter name. Adding *, makes that parameter an iterable and allows to store multiple values which can be iterated over or accessed using index number. Please see below example.
def MyFunction(*num): FinalValue = 1; for i in num: FinalValue = FinalValue * i return FinalValue print(MyFunction(2, 3)) print(MyFunction(2, 3, 5))
6 30
A function which can call itself is termed as recursive function. A recursive function generally ends with one or more boundary conditions.
def factorial(num): #checking whether the parameter value is positive integer or not if(isinstance(num, int) and num >=0): if(num > 0): return num*factorial(num-1) else: return 1 else: return "Number should be positive Integer." print(factorial(5)) print(factorial(2.5)) print(factorial(-3))
120 Number should be positive Integer. Number should be positive Integer.