Python Tutorial Python Advanced Python References Python Libraries

Python - Functions



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. Python has many built-in functions 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, and function definition.

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 example below, a function HelloWorld is created to print Hello World!. It requires no parameters to execute.

def HelloWorld():
  print("Hello World!")

HelloWorld()

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 example below, 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 the default value.

Example: A function with a default value parameter

In the example below, 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 example below 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. See the example below:

Example: A function with a default value parameter

In the example below, 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. The function returns the product of these two arguments.

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

Function with Named Parameter

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

Example:

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

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

MyFunction(num1 = 5, num2 = 10)
MyFunction(num2 = 10, num1 = 5)

The output of the above code will be:

0.5
0.5

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. See the example below:

Example: Function to multiply unknown number of parameters

In the example below, 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