Functions in python

Functions are sets of statements that perform a specific task. They can be called by their names, more than once in a program. Inputs can be given to functions based on which they will perform a task and can give back the result. Functions avoid same code to be written over and over again, thus, reducing the redundancy of the code. They also modularize programs by assigning one task for one function. It also makes a program easy to correct. We would need to correct the code at only one place when a function is not working as desired as compared to situation where all the places where that set of code was written has to be edited.

The basic syntax of a function is as follows:

Example code syntax
def func(inputs):  
    statement 1  
    statement 2 
    .  
    .  
      
    return result

For example, a function can be written that return the result of addition of two numbers. It takes two numbers, a and b as input and returns the result. It can be written in a few ways:

Python
def add_num(a,b):
    c = a + b
    return c

The above function is named as add_num. It takes two inputs, a and b. The inputs of a function are called as arguments.

Inside the function, a variable c is created in which the result of addition of the two numbers given to the function is stored. The variable c is returned back.

We can write this function without creating the variable c and directly using the return statement.

Python
def add_num(a,b):
    return a + b

How to call a function

After you have written down the code of the function, the function can be called in the program by its name. For example, the add_num function can be called like this.

Python
print(add_num(2,3))
Output
5

We can also store the output of a function in a variable.

Python
c = add_num(2,3)
print(c)
Output
5

The arguments of a function can be some variables.

Python
p = 2
q = 3
r = add_num(p, q)
print(r)
Output
5

Positional and keyword arguments

In the above code, python interprets the arguments to the function relative to their position in the definition of the function, i.e., argument p will take the value of a inside the function and the argument q will take the value of a.

These are called positional arguments.

We can give argument of a function by their keywords. So, we can write the arguments in any order. This can be demonstrated by writing a function that prints out its inputs.

Python
def area(base, height):
    print('base = ', base)
    print('height = ', height)
    
area(base=3, height=6)
Output
base =  3
height =  6

We will call the function again with swapping the position of the arguments using their names.

Python
area(height=6, base=3)
Output
base =  3
height =  6

So, no matter in what order we write the arguments, using keywords we can define which argument of the function would take which value.

Built-in functions of python

The add_num function is one we created. Python language has several built-in functions. One of it is len which we have seen in the post where we discussed dictionaries and lists. With len function we can see the length of a string, list or a dictionary.

Python
alist = [1,2,3] # a list
list_length = len(alist)
print(list_length)
Output
3
Python
astring = 'elephant'
string_length = len(astring)
print(string_length)
Output
8

Default values for function inputs

Inputs (arguments) of a function can have default values. When we call the function in a program and not give the argument, its default value can be used to execute the function.

We can can write the function div_num by giving default value of 2 to the argument b. We will rename the function to div_num_2 for clarity.

Python
def div_num_2(a, b=2):
    return a/b

When we call the function with just one argument the default value of b is taken.

Python
r1 = div_num_2(6)
print(r1)
Output
3.0