Appendix B — Function basics

In this chapter we will explain some basic commands for Python functions defined using def.

B.1 Input arguments

Consider the function below.

def f(x,a,b,c):
    return a*x**2 + b*x + c

There are various ways to execute this function for given input values. First of all, we can execute the function with positional input arguments.

y = f(-1,2,1,3)

print(y)
4

In the above function call, Python assigns the first input 1 to the first local variable x of f, the second input 2 to second local variable a, the third input 1 to local variable b, and the fourth input -3 to local variable c.

Alternatively, we could also have inputted the numbers as keyword (or named) input arguments, where we explicitly assign every input to its corresponding local variable of f.

y = f(x=-1,a=2,b=1,c=3)

print(y)
4

The advantage of the this approach is that the order in which we input the arguments does not matter. This can be useful if a function has many inputs, but you do not want to remember their (positional) ordering in f.

y = f(a=2,c=3,b=1,x=-1)

print(y)
4

It is also possible to use a mixture of positional and keyword arguments. If you do this, then the positional arguments should always come first, followed by the keyword arguments in any order you like.

If a function f has n input arguments, and you use k positional input arguments, then these are assigned to the first k local variables of your function. The remaining n - k keyword arguments should then refer to the the last n - k input arguments of f.

y = f(-1,2,c=3,b=1) 

# Examples of what does not work
# f(-1,c=3,2,b=1) 
# f(-1,2,a=3,b=1)

print(y)
4

Finally, it is also possible to input all arguments in a list (or array), which can then be unpacked in the function. Unpacking here means that the elements of the list are assigned to the individual local variables of the function.

If the input values are stored in a list called vars, then you can unpack them by using the argument *vars in the function.

# Values of input arguments for function
vars = [-1,2,1,3]

# Unpack variables and assign to local variables in positional sense
y = f(*vars)

# Print function value
print(y)
4

B.2 Output arguments

Suppose we have a function that takes two inputs, and yields four outputs.

def f(a,b):
    return a + b, a - b, a*b, a/b

a = 5
b = 3

There are various ways to get one or more specific outputs from Python.

# Returns all output variables in tuple
output = f(a,b)

print(output)
(8, 2, 15, 1.6666666666666667)
# Store outputs in variables w, x, y and z
w, x, y, z = f(a,b)

print(w, x, y, z)
8 2 15 1.6666666666666667

We can suppress one or more output arguments using _.

# Only store first and last output
w, _, _, z = f(a,b)

print(w, z)
8 1.6666666666666667

If we only want the first output, and suppress the other ones, we can use *_

# Only store first output
x, *_ = f(a,b) # This is the same as x, _, _, _ = f(a,b)

print(x)
8

If we only want the first two outputs, and suppress the remaining ones, we can do something similar.

# Only store first output
w, x, *_ = f(a,b) # This is the same as w, x, _, _ = f(a,b)

print(w,x)
8 2

If we only want the last output, we can do the following.

# Only store last output
*_, z = f(a,b) # This is the same as _, _, _, z = f(a,b)

print(z)
1.6666666666666667

Also here, if we would like the last two outputs, we can do the following.

# Only store last output
*_, y, z = f(a,b) # This is the same as _, _, y, z = f(a,b)

print(y,z)
15 1.6666666666666667