1.12 functions
Contents
Note
Click here to download the full example code or to run this example in your browser via Binder
1.12 functions#
Important
This lesson is still under development.
A function is a box which takes something as input and it gives you output after performing some operations on that input. In python, the input and output arguments for a function are optional. The basic syntax of a minimal function in is as below
def FunctionName(InputArguments):
commands to find output
return output
As said earlier, InputArguments and return
are optional. This means we can write
a valid function in python without input arguments or a function which does not
return something.
def add_nums():
pass
print(add_nums)
<function add_nums at 0x7ffad3f37940>
type(add_nums)
If we want to use/run a function, this means we want to call it. Following syntax can be used to call a function
Output = FunctionName(InputArguments)
Output
is optional and so does InputArguments
add_nums()
The following function takes two input arguments and adds them
def add_nums(a, b): # for brevity, we can not write add_nums(a+b):
print('a: ', a, ' b:', b)
a + b
add_nums(1, 5)
a: 1 b: 5
Inside the function, we name the two input arguments as a and b.
a: 12 b: 14
Outside function 12 is x but inside function, 12 is a. The variable a is not available outside the function.
a: 12 b: 14
26
Above we we created c inside the function and returned it. Outside the function when we called the function, this value of c is printed. The variable named c is not available itself. We can assign this value to a new variable whose name is z as shown below below. It can be any legal variable name. The variables which can be seen inside the function and which can be seen outside the function will be covered under the topic 1.14 global vs local.
a: 12 b: 14
The function returns z: 26
The variables c and z have same values with the difference that c exists only inside the function. Moreover, the creation of c is not necessary, we can just return the result as it is.
a: 12 b: 14
The function returns z: 26
We can provide default values to input arguments. The default values of input arguments are only used if we don’t provide their values when calling them, otherwise their values are overwritten.
a: 114 b: 313
The function returns z: 427
Above: The value from variable x is going to a and will replace its default value. The value from variable y will go to b and will overwrite its default value i.e. 14.
When we have defined the default values of input arguments in function definition, we can skip one or more input arguments when calling the function as shown below.
a: 313 b: 14
The function returns z: 327
Above, the value from y will be assigned to a while b will use its default value.
a: 12 b: 313
The function returns z: 325
Above, a will use its default value while b will get value of y.
a: 114 b: 14
The function returns z: 128
Above: a will use the value of x i.e. 114 while b will use its default value i.e. 14.
a: 1 b: 14
The function returns z: 15
Above, 1 will go to a while b will use its default value. The function can also be called without providing any input argument because both input arguments are optional. In this case the default values will be used.
a: 12 b: 14
The function returns z: 26
We can define the optional arguments with obligatory arguments. In function below, c is optional, while a and b are obligatory.
a: 1 b: 2 c: 14
The function returns z: 3
Above, we have not provided the value for _c. As it was optional argument, its default value was used.
Question: What will be the output of following function?
# z = add_nums()
# print('The function returns z: ', z)
Question: Guess the output from following cell.
# z = add_nums(a=1, c=313)
# print('The function returns z: ', z)
Return#
A function returns None
by default.
import random
qatleen = ['winsten churchil', 'rana sanaullah', 'obama', 'musharaf']
def print_qatal():
print(random.choice(qatleen))
return None
x = print_qatal()
type(x)
winsten churchil
Even if a function does not return anything explicitly, it still returns None.
def print_qatal():
print(random.choice(qatleen))
return
x = print_qatal()
type(x)
rana sanaullah
Ff a function does not have return
statement, it still returns None
.
def print_qatal():
print(random.choice(qatleen))
x = print_qatal()
type(x)
musharaf
So it is impossible in python to write a function which returns absolutely nothing.
If a function returns more than one object/variables, and we assign it
to a single variable, then this new variable will be tuple
.
(5, 12) <class 'tuple'>
Above: The function returns 2 arguments but we assigned it to 1 variable named xx. Thus, xx will be a tuple consisting of two values
We can however, assign both returned values from a function to two new variables as shown below. %%
5 <class 'int'>
12 <class 'int'>
If the function returns fewer or more arguments than the variables assigned, then it will give error.
# uncomment the following line
# xx, yy, zz = func(5, 12) # Error
If the function returns multiple values but we want to get only first value, we can do it as below
12
return a tuple from function
<class 'list'>
<class 'tuple'>
<class 'tuple'>
<class 'int'>
The reason is that it is comma ,
which makes something a tuple not the brackets.
So in order to get a tuple from function, we must put comma even if
there is only one object in the tuple as shown below.
2
[4, 6]
A common way to ignore the unncessary output from a function is to use underscore
Above we were interested in only var and wanted to ignore everything else returned by return_tuple function.
function as input argument#
We can assign a function to a variable and use that variable to call the function.
This goes into print_me
Thus we can use functions as input arguments to other functions as well.
def magic(left, op, right):
return op(left, right)
def my_op(var_a, var_b):
return var_a == var_b
magic(2, my_op, 2)
True
break vs return#
6.0
As soon as the value of _sum became greater than break_point, the function exited and we got the value of _sum at that point.
15.0
Was the break
statement executed above?
docstring#
The first string inside the functions is usually put for help. This is
called docstring. It can be called by __doc__
method
def fahrenheit(T_in_celsius):
"""This function converts temperature from Celsius to Fahrenheit. """
return (T_in_celsius * 9 / 5) + 32
fahrenheit.__doc__
'This function converts temperature from Celsius to Fahrenheit. '
help(fahrenheit)
Help on function fahrenheit in module __main__:
fahrenheit(T_in_celsius)
This function converts temperature from Celsius to Fahrenheit.
If we want to know the name of a function as string we can do it as following.
converter = fahrenheit
print(converter.__name__)
fahrenheit
Total running time of the script: ( 0 minutes 0.015 seconds)