Facebook Page Twitter Page LinkedIn Page
× Python Tutorial Python Advanced Python Resources Python Libraries


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.

Create Function

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.

Call Function

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.

Syntax

#Defining function
def function_name(parameters):
    statements

#Calling function
function_name(parameters)

Example: A function with no parameter

In the below example, a function called MyFunction is created to print Hello World!. The function requires no parameters to execute.

def MyFunction():
  print("Hello World!.")

MyFunction()

The output of the above code will be:

Hello World!.

Parameter

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.

Example: A function with a parameter

In the below example, the function called MyFunction is created which takes one parameter to execute.

def MyFunction(name):
  print("Welcome to Python Programming! " + name +".")

MyFunction("John")
MyFunction("Marry")
MyFunction("Sam")

The output of the above code will be:

Welcome to Python Programming! John.
Welcome to Python Programming! Marry.
Welcome to Python Programming! Sam.

Default Parameter Value

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.

Example: A function with a default value parameter

In the below example, the function called MyFunction is created which takes one parameter to execute. If the parameter is not passed it takes the default value of the parameter.

def MyFunction(name = "Dude"):
  print("Welcome to Python Programming! " + name +".")

MyFunction()
MyFunction("John")

The output of the above code will be:

Welcome to Python Programming! Dude.

Welcome to Python Programming! John.

Example: Passing iterable in a function

The below example describes how to pass an iterable as a parameter in the function.

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))

The output of the above code will be:

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.

Function to Return Values

A function can be used to return values. Please see example below:

Example: A function with a default value parameter

In the below example, the function called MyFunction is created which takes two parameter to execute: num and multiplier. The default value of multiplier parameter is 2. If the multiplier parameter is not passed, function will take its default value.

def MyFunction(num, multiplier = 2):
  x = num * multiplier
  return x 

print(MyFunction(5, 3))
print(MyFunction(10))
print(MyFunction(25, 4))

The output of the above code will be:

15

20

100

Key Parameter

Parameter values can be passed in function with key = value syntax also. In this way, the order of parameter does not matter.

Example:

The below example describes how to pass parameters in a key = value pair.

def MyFunction(num1, num2):
  print(num1/num2)

MyFunction(num2 = 2, num1 = 10)

The output of the above code will be:

5.0

Unknown number of Parameters

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.

Example: Function to multiply unknown number of parameters

In the below example, function called MyFunction is created which can be used when there is no information about number of parameters.

def MyFunction(*num):
  FinalValue = 1;
  for i in num:
    FinalValue = FinalValue * i
  return FinalValue

print(MyFunction(2, 3))
print(MyFunction(2, 3, 5))

The output of the above code will be:

6

30

Recursive function

A function which can call itself is termed as recursive function. A recursive function generally ends with one or more boundary conditions.

Example: A function for calculating factorial using recursion method

The below example, a recursive function called factorial is created which returns factorial of the passed parameter.

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))

The output of the above code will be:

120

Number should be positive Integer.

Number should be positive Integer.