Hello friends, Today we will be learning about Functions in python.
Are you new here??
We recommend reading previous chapters for better understanding.
- Introduction to programming – Day1
- Introduction to Algorithm – Day2
- Python Variable and operators – Day 3
- Python Control Structure(if-else, loops) – day4
A function is a block of code that performs a particular task. In python, functions are declared using the keyword def.
Observe the output:
observe1="What's happening!!" def passport_check(passport_no): #function execution start if(len(passport_no)==8): if(passport_no>="A" and passport_no<="Z"): status="valid" else: status="invalid" else: status= "invalid" observe6="func. execution ends" return status passport_status=passport_check("M9993471") 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(): statements . . return [expression]
Note: Return statement can not be used outside the function.
Let’s see how we can use values returned from a 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
Do it yourself.
Write function call statement.
def check_value(message,num): msg=message[:num] return len(msg) #function call statement print("Result:", result)
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.
Find the output:
def verify(num1,num2): if num1 > num2: return num1 elif num1 == num2: return 1 else: return num2 def display(arg1,arg2): if(verify(arg1,arg2)==arg1): print("A") elif(verify(arg1,arg2)==1): print("C") else: print("B") display(1000,3500)
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): boarding_pass="Issued" def update_seat(seat_list): seat_list=25 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=["Jack","NA"] print("passenger_seat before function call:", passenger_seat) update_seat(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
- Variable argument count
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
Allow flexibility in the order of passing the actual arguments by passing the argument name.
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
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.
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