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 assigining 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 wirtten has to be edited.
The basic syntax of a function is as follows:
def func(inputs):
1
statments
.
.
.
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:
def add_num(a,b):
= a + b
c return c
The above function is named as add_num
and 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 number inputted 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
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:
print(add_num(2,3))
5
We can also store the output of a function in a variable.
= add_num(2,3)
c print(c)
5
In real programs the arguments of a function are going to be some variables.
= 2
p = 3
q = add_num(p, q)
r print(r)
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 for division of two numbers first.
def div_num(a,b):
return a/b
We will fist define the two numbers.
= 8
p = 4 q
Calling positional arguments would be as follows:
= div_num(p, q)
r print(r)
2.0
We wil call the function with keyword arguments i two ways to generate different results.
First we will assign a=p
and b=q
which will
give the result 2
as above.
= div_num(a=p, b=q)
r print(r)
2.0
Now, we will use the value of q
to the argument
a
and p
to the argument b
.
= div_num(a=q, b=p)
r print(r)
0.5
We can also write the function by swapping the order in which we write the arguments to the function.
= div_num(b=p, a=q)
r print(r)
0.5
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.
= [1,2,3] # a list
alist = len(alist)
list_length print(list_length)
3
= 'elephant'
astring = len(astring)
string_length print(string_length)
8
Default values for inputs
Inputs (arguments
) of a function can have default
values. So, 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 clearity.
def div_num_2(a, b=2):
return a/b
So, when we call the function with just one argument the default
value of b
is taken.
= div_num_2(6)
r1 print(r1)
3.0