Pass by reference versus pass by value
Pass by reference is the term used in some programming languages, where values to the argument of the function are passed by reference, that is, the address of the variable is passed and then the operation is done on the value stored at these addresses.
Pass by value means that the value is directly passed as the value to the argument of the function. In this case, the operation is done on the value and then the value is stored at the address.
In Python arguments, the values are passed by reference. During the function call, the called function uses the value stored at the address passed to it and any changes to it also affect the source variable:
def pass_ref(list1): list1.extend([23,89]) print "list inside the function: ",list1 list1 = [12,67,90] print "list before pass", list1 pass_ref(list1) print "list outside the function", list1
Here, in the function definition, we pass the list to the pass_ref
function and then we extend the list to add...