Pass by value vs pass by reference in python using function
Generally, all the arguments written in python are pass by reference mostly but if you need to change the code according to requirement then it is also possible in it but the changes will get reflected the calling function also.
Example1:
#Changes will be reflected outside also
def modifyme( my ):
my.append([1,2,3,4]);
print ("Values inside the function will be: ", my)
return
# Now you can call changeme function
my = [109,204,303];
modifyme( my );
print ("Values outside the function will be: ", my)
Example2:
def modifyme( my ):
my=[1,2,3,4];
print ("Values inside the function will be: ", my)
return
# Now you can call changeme function
my = [109,204,303];
modifyme( my );
print ("Values outside the function will be: ", my)
Global variables vs local variables:
Global variables are those variables that are defined outside the scope i.e. outside block or function. Local variables are those types of variables which are defined location and there scope is also local only. We can access the global variable from anywhere in the program but the local variable is fixed scope.
Examples:
outvariable = 0; # This is global variable.
# Function definition is here below
def sumfunction( arg1, arg2 ):
# Add both the parameters and return them."
total = arg1 + arg2; # Here total is local variable.
print ("Inside the function local total : ", total)
return total;
# Now you can call sum function
sumfunction( 10, 20 );
print ("Outside the function global total : ", outvariable)
