Function in Python

Function in Python

In earlier tutorial, we have studied about fundamentals of python programming. Now we are going to read the concept of Function in Python.

So first of all what is function: in simple terms we can say that “A function is block of reusable code used to perform single or related actions/tasks.

Benefit of the function is, it breaks larger program into smaller and modular chunks and thus makes program more readable and manageable

Syntax : Defining a function in Python

def func_name(parameters):

Important point about function

  • Keyword def is used to start the function block in python.
  • func_name(parameters) is used to give name to the function and pass the list of parameters(optional).
  • “ : ” is used to close the function header.
  • A return statement is used to return value from the function and it is optional.
  • All the statements inside the function should be indented using equal spaces. Function can accept zero or more arguments(also known as parameters) enclosed in parentheses.

Calling a defined function

we can call defined function by its name followed by arguments list in closing brackets(if there is any).


Example of a Function that add numbers

[pastacode lang=”python” manual=”def%20sum_func(start%2C%20end)%3A%0A%20%20%20total%20%3D%200%0A%20%20%20for%20i%20in%20range(start%2C%20end%20%2B%201)%3A%0A%20%20%20%20%20%20%20total%20%2B%3D%20i%0A%09%0A%20%20%20print(total)%0A%20%0Asum_func(5%2C%2010)” message=”Function for summation of given range of values” highlight=”” provider=”manual”/]

Output : 45  (5+6+7+8+9+10)

In the above example, we have defined a function sum_func(start, end). It takes two values as arguments, one to initialize the range and another to end the range. This function will take summation of all the numbers that is starting from 5 till 10. Because at the time of calling of function we give start = 5 and end = 10 .

Return statement in Python

After execution of the function body, return statement is used to return the result to the caller and exists the function.

Example of function using return statement 

[pastacode lang=”python” manual=”def%20sum_func(start%2C%20end)%3A%0A%20%20%20total%20%3D%200%0A%20%20%20for%20i%20in%20range(start%2C%20end%20%2B%201)%3A%0A%20%20%20%20%20%20%20total%20%2B%3D%20i%0A%20%20%20return%20total%20%0A%0As%20%3D%20sum_func(5%2C%2010)%0Aprint(s)” message=”Function with return statement” highlight=”” provider=”manual”/]

Output : 45

Scope of the Variable: Global vs Local

  • Global variable: Can be used throughout the program and can be accessed by all function.
  • Local Variable:  Variables which are declared and used inside a function are called local variables.

Program to understand the scope of the variable:

[pastacode lang=”python” manual=”global_var%20%3D%2012%20%20%20%20%20%20%20%20%20%23%20a%20global%20variable%0A%20%0Adef%20func()%3A%0A%20%20%20%20local_var%20%3D%20100%20%20%20%20%20%23%20local%20variable%20inside%20func()%0A%20%20%20%20print(global_var)%20%20%20%23%20you%20can%20always%20access%20global%20variables%20in%20side%20of%20function%0A%20%0Afunc()%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20calling%20function%20func()%0A%20%0A%23print(local_var)%20%20%20%20%20%20%20%20%23%20you%20can’t%20access%20a%20local_var%20outside%20the%20function%2C%20because%20as%20soon%20as%20function%20ends%20local_var%20is%20destroyed.” message=”Program showing scope of variable” highlight=”” provider=”manual”/]

Output : 12

Program when same variable name is used inside the function and outside the statement.

[pastacode lang=”python” manual=”same_var%20%3D%20500%0A%20%0Adef%20test()%3A%0A%20%20%20%20same_var%20%3D%20100%20%20%20%20%20%23%20same_var%20inside%20the%20function%20is%20totally%20different%20from%20same_var%20outside%20the%20function%0A%20%20%20%20print(same_var)%20%20%20%20%23%20this%20will%20print%20local%20same_var%20variable%20i.e%20100%0A%20%0Atest()%0A%20%0Aprint(same_var)%20%20%20%20%20%20%20%20%23%20this%20will%20print%20global%20same_var%20variable%20i.e%20500″ message=”Understating more about Global and Local variables ” highlight=”” provider=”manual”/]

Output : 100


Note: we can define a global variable inside a function using global keyword.

[pastacode lang=”python” manual=”def%20test()%3A%0A%20%20%20%20global%20x%20%20%20%23%20x%20is%20declared%20as%20global%20so%20it%20is%20available%20outside%20the%20function%20%0A%20%20%20%20x%20%3D%20100%20%0A%09%0Atest()%0Aprint(x)” message=”Defining a global variable inside the function” highlight=”” provider=”manual”/]

Output : 100

Program for function having default values for arguments

[pastacode lang=”python” manual=”def%20func(i%2C%20j%20%3D%20100)%3A%0A%20%20%20%20print(i%2C%20j)%0A%0Afunc(2)%20%20%20%20%20%20%20%20%20%20%23%20here%20no%20value%20is%20passed%20to%20j%2C%20so%20default%20value%20will%20be%20used%0A%0Afunc(2%2C%20300)%20%20%23%20here%20300%20is%20passed%20as%20a%20value%20of%20j%2C%20so%20default%20value%20will%20not%20be%20used” message=”Default value for argument inside a function” highlight=”” provider=”manual”/]

output: 2  100
        2  300

Returning multiple values from Function using return statement

In python, we can return multiple values from a Function using return statement by separating them with comma (,). Multiple values will be returned as Tuple.

[pastacode lang=”python” manual=”def%20bigger(a%2C%20b)%3A%0A%20%20%20%20if%20a%20%3E%20b%3A%0A%20%20%20%20%20%20%20%20return%20a%2C%20b%20%20%20%23%20Returning%20two%20values.%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20return%20b%2C%20a%0A%20%0As%20%3D%20bigger(12%2C%20100)%0Ap%20%3D%20bigger(250%2C14)%0Aprint(s)%0Aprint(p)%0Aprint(type(s))%20%20%20%20%20%20%20%23Checking%20the%20type%20of%20the%20varible%20s.” message=”Returning multiple values from function” highlight=”” provider=”manual”/]

Output: (100, 12)
        (250, 14)
      <class 'tuple'>

Types of function

Basically, we can divide functions into the following types:

  1. User-defined functions– Functions defined by the users as we have seen so far in all those examples.
  2. Built-in functions – Functions that are built into Python like print(), abs(). Etc.


Next Tutorial

Built-in functions in Python

One Reply to “Function in Python”

Leave a Reply