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.
In Python, we don't have to think about pass by value and pass by reference as it does that automatically for you. To emulate this using Python, we use the concept of mutability. If the argument passed is immutable then it follows pass by value, else if the argument passed is mutable then it follows pass by reference.
Note : Till now we have seen int, float, string data types which immutable and mutable data types we will discuss in later part of the course.
Execute the below code in python playground and observe output, which emulates the behavior of pass by value and pass by reference.
def change_number(num): num+=10 def change_list(num_list): num_list.append(20) num_val=10 print("*********effect of pass by value*********") print("num_val before function call:", num_val) change_number(num_val) print("num_val after function call:", num_val) print("-----------------------------------------------") val_list=[5,10,15] print("*********effect of pass by reference*********") print("val_list before function call:", val_list) change_list(val_list) print("val_list after function call:", val_list)
Programming languages allow controlling the ordering and default values of arguments.
In python we will observe the following:
Tryout the below code in python playground and observe the results.
def display1(flight_number, seating_capacity): print("Flight Number:", flight_number) print("Seating Capacity:", seating_capacity) print("code-1: positional arguments") display1("FN789",200) #Uncomment and execute the below function call statement and observe the output #display1(300,"FN123") def display2(flight_number, seating_capacity): print("Flight Number:", flight_number) print("Seating Capacity:", seating_capacity) print("-------------------------------------------------") print("code-2: keyword arguments") display2(seating_capacity=250, flight_number="FN789") def display3(flight_number, flight_make="Boeing", seating_capacity=150): print("Flight Number:", flight_number) print("Flight Make:", flight_make) print("Seating Capacity:", seating_capacity) print("-------------------------------------------------") print("code-3: default arguments") display3("FN789","Eagle") #Uncomment and execute the below function call statements one by one and observe the output #display3("FN234") #display3("FN678","Qantas",200) def display4(passenger_name, *baggage_tuple): print("Passenger name:",passenger_name) total_wt=0 for baggage_wt in baggage_tuple: total_wt+=baggage_wt print("Total baggage weight in kg:", total_wt) print("-------------------------------------------------") print("code-4: variable argument count") display4("Jack",12,8,5) #Uncomment and execute the below function call statements one by one and observe the output #display4("Chan",20,12) #display4("Henry",23)
The below code has been written to represent the baggage weight check process based on the weight limit specified by an airline.
Go through the below code and guess the output.
wt_limit=30 def baggage_check(baggage_wt): extra_baggage_charge=0 if not(baggage_wt>=0 and baggage_wt<=wt_limit): extra_baggage=baggage_wt-wt_limit extra_baggage_charge=extra_baggage*100 return extra_baggage_charge def update_baggage_limit(new_wt_limit): wt_limit=new_wt_limit print("This airline now allows baggage limit till",wt_limit,"kgs") print("This airline allows baggage limit till",wt_limit,"kgs") print("Pay the extra baggage charge of",baggage_check(35),"rupees") update_baggage_limit(45) print("Pay the extra baggage charge of",baggage_check(35),"rupees")
Let us go through the code now.
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 till owning function finishes execution. They can be accessed only inside that function.
wt_limit is created outside the functions. Hence it is a global variable. Global variables are created when the program execution starts and remains in memory till the program terminates. They can be read anywhere in the program - within a function or outside. But they are protected from modification inside a function. As it is available throughout the program, use of global variable should be restricted to avoid accidental misuse by developers and to minimize memory usage.
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.
All Rights Reserved. © 2024 BookOfNetwork