Function Parameters and Return Statement in Python

In this class, we discuss function parameters and return statement in python.

For Complete YouTube Video: Click Here

Memory Allocation to Function Parameters

Before going to understand different parameters available in python.

First, we have to understand few concepts on how memory is allocated to function parameters.

Understanding memory allocation to function parameters will help you a lot in improving your coding skills.

The reader should have knowledge on how memory allocated to variables. Click here.

Let’s take some examples and understand the concept of memory allocation to parameters deeply.

Example 1:

def f(x):
    print("the number passed is",x)
    print(id(x))   
a=1
print(id(a))
f(a)
Output: 
8684480
the number passed is 1
8684480

From the above example, variable a is having an integer value.

Assume variable a is assigned a memory location 50.

How memory allocated to data types? Click here.

Now function f is called. Variable a is sent during the function call.

The reference of variable a is sent to the function. The parameter x in function f will take the reference.

Variable x in function f is also referencing memory location 50.

Point to understand. In python, when we call a function. Always references of arguments are sent.

The above program displays both variables that refer to the exact memory location.

Example 2:

def f1(x):
    x.append([1,2])
    print(id(x))
    print(x)
a=[5,6]
f1(a)
print(a)
print(id(a))
Output:
3028582732
[5,6,[1,2]]
[5,6,[1,2]]
3028582732

From the above program variable, a is pointing to a list object.

Variable a is sent to the function f. the reference of variable a is sent here.

Now function parameter x is also referencing to the same list referencing by variable a.

The list is modified in the function as the list is a mutable object.

After modifying the object. Function parameter Variable x and variable a both pointing to the same object.

To know more about mutable and immutable objects. Click here.

After modifying the list in the function, we displayed the variable a., which will display the modified list [5,6,1,2].

The above situation is not the same when we pass immutable objects to the function. The example is given below.

Example 3:

def f2(x):
    x=x+1
    print(x)
    print(id(x))
a=1
f2(a)
print(id(a))
print(a)
Output:
2
8684496
8684480
1

In the above program variable, a is sent to the function f2.

Variable a referencing to integer object. Parameter x will also refer to the same memory referenced by variable a.

In the function, the value in parameter x is modified.

Because an integer object is immutable, a new object is created, and the new object reference is given to parameter x.

Types of Function Parameters

Required Arguments

def f(x,y):
    z=x+y
    print(z)
a=1
b=2
f(a,b)
Output:
3

In the above program, the function has two parameters.

When we call the function, we have to send two arguments. The argument is assigned to parameters from left to the right.

In our example, a is assigned to parameter x.

b is assigned to parameter y.

Keyword Arguments

def f(x,y):
    z=x+y
    print(z)
a=1
b=2
f(y=b,x=a)
Output:
3

In the above example, We mentioned variable b should be assigned to parameter y.

The argument a is assigned to parameter x. Here no need to maintain the order.

We assigned using parameter names.

Default Arguments

def f(x,y=5):
    z=x+y
    print(z)
a=1
f(a)
Output:
6

In the above program y parameter in function f has a default value of 5.

If parameter y is not assigned with any argument during the function call, then parameter y is assigned with a default value.

Variable Length Arguments

def f(x, *y):
    print(x)
    print(y)
    for i in y:
        print(i)
f(1)
print("************")
f(1,2,5,6,7)
Output:
1
()
************
1
(2,5,6,7)
2
5
6
7

In the above program, the parameter y is given a star before.

The meaning of the star is to take any number of arguments.

All the arguments for parameter y are taken in a tuple.

Function f is called with arguments 1,2,3,4,5,6.

Argument 1 is assigned to parameter x., and the remaining arguments are assigned to parameter y.

return Statement

def f(x,y=5):
    z=x+y
    return z
a=1
k=f(a)
print(k)
Output:
6

If a function wants to return a value. We use a return statement.

In the program given above. Function f is returning the variable z.

Here reference of variable z is returned.

During the function call, we have to write k =f(a).

The returned reference is assigned to variable k.