Functions in Python – 5th Day of Python Series

Hello friends, Today we will be learning about Functions in python.

Are you new here??

We recommend reading previous chapters for better understanding.

A function is a block of code that performs a particular task. In python, functions are declared using the keyword def.

syntax of function in python
Syntax of function in python
python functions
Function signature and Function call

Observe the output:

observe1="What's happening!!"

def passport_check(passport_no):
    #function execution start
        if(passport_no[0]>="A" and passport_no[0]<="Z"):
        status= "invalid"
    observe6="func. execution ends"
    return status


print("Passport is",passport_status)

But, Why we need functions?

The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can call the function.

Python provides built-in functions like print(), etc. but we can also create your own functions. These functions are called user-defined functions.

How we return value from function?

Let’s see how we can use return in different ways in a function.

Python Return Statement

return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned.

def fun():
    return [expression]
how to return value from function

Note: Return statement can not be used outside the function.

Let’s see how we can use values returned from a function.

return in python function

Some notable points of functions:

  • It is a section of a program that performs a specific task.
  • It helps to clearly separate tasks within a program
  • Control can be transferred back and forth between different functions based on need
  • It helps to achieve modularity
  • It supports reusability.

Actual and formal argument in python

actual and formal argument in python function

Do it yourself.

Write function call statement.

def check_value(message,num):
    return len(msg)

#function call statement
print("Result:", result)
def function_name([arg1,…,argn]):
    [return value]
variable_name = function_name([val1,…,valn])

Is it possible to call a function from another function.

Yes, It is possible to call a function from another function.

Observe that:

function syntax in python
function syntax in python

Find the output:

def verify(num1,num2):
    if num1 > num2:
        return num1
    elif num1 == num2:
        return 1
        return num2

def display(arg1,arg2):


Pass by value and Pass by Reference

In programming, there are two ways in which arguments can be passed to functions: pass by value and pass by reference.
Some languages use pass by value by default while others use pass by reference. Some languages support both and allow you to choose.

Pass by value

Pass by reference

So, which passing mechanism is used in python.

If we pass immutable arguments like integers, strings or tuples to a function, the passing acts like Call-by-value. It’s different if we pass mutable arguments.

Observe the output:

def check_in(baggage,boarding_pass):
    if(baggage>=1 and baggage<=30):

def update_seat(seat_list):

boarding_pass="Not Issued"
print("boarding_pass before function call:", boarding_pass)
check_in(25, boarding_pass)
print("boarding_pass after function call:", boarding_pass)
print("boarding_pass, a string is immutable")

print("passenger_seat before function call:", passenger_seat)
print("passenger_seat after function call:", passenger_seat)
print("passenger_seat, a list is mutable")


boarding_pass before function call: Not Issued
boarding_pass after function call: Not Issued
boarding_pass, a string is immutable
passenger_seat before function call: ['Jack', 'NA']
passenger_seat after function call: ['Jack', 25]
passenger_seat, a list is mutable

Python Function Arguments – Ordering and default arguments

  1. Positional
  2. keyword
  3. Default
  4. Variable argument count
Python function argument

Positional Argument

It is a default way of specifying arguments.

The order, count and type of actual argument should exactly match that of the formal argument.yword

Keyword Argument

Allow flexibility in the order of passing the actual arguments by passing the argument name.

Default Argument

Allow to specify a default value in the function signature. It is used when no value is passed for that argument else it works normally.

Varying length argument

Allow a function to have variable number of argument

Variable and Its scope in python function

global variable in python function
Variable and Its scope

extra_baggage and extra_baggage_charge are created inside the function baggage_check().

Hence they are local to that function or in other words, they are local variables.

They are created when owning function starts execution and remains in memory until owning function finishes execution.

They can be accessed only inside that function.

In cases where a global variable needs to be modified inside a function, like in function update_baggage_limit(), Python allows you to do that using the global keyword.

variable and their scope in python function
local variables

Video on functions in python


Here is a summary of what we learnt so far today about functions.

  • Arguments to functions
    • Pass by value, pass by reference
  • Mutable, immutable arguments
  • Function arguments – Ordering and default values
    • Positional, Keyword, Default and Variable argument count
  • Variables and its scope
    • Local and Global

Practice here

function in python
Thank You for visiting our website

What do you think?

Written by My Inquisitor


Leave a Reply

GIPHY App Key not set. Please check settings




Python Control Structures – 4th Day of Python Series

Why should we choose Java language 1st?