Passing Arguments to Function and Return in Python

Passing arguments to Function in Python programming and using Return keyword to return value. Functions are used to divide a large program into sub-programs for easy control.

Program Counting Function Called Times

Program

def bookie(func): #passing here argument.
     func.count += 1 #adding 1.
def myfunc():
     bookie(myfunc) #passing argument.
     #Some other code goes here.
myfunc.count = 0
for i in range(10): #loop looping from 0 to 9.
     myfunc() #calling function.
print(myfunc.count) #printing.
Output
10

Program Passing Arguments to Parameters and Returning Value

Program

def fun(a, b): #passing here arguments to parameters.
    c = a + b
    return c #returning 'c' value.
print(fun(5, 6)) #calling and passing arguments to parameters and
                #printing Returned value.
Output
11

Program Passing Arguments and Returning Value Another Method

Program

def fun(a, b): #passing here arguments to parameters.
    c = a + b
    return c #returning 'c' value.
val = fun(5, 6) #calling and passing arguments to parameters and
              #assigning Returned value to 'val'.
print("The Returned value is: ", val) #and printing.
Output
The Returned value is: 11