Functions

Function definition

Definition: A function in Python is the programmatic realization of the idea of a math function $f:A \rightarrow B$, that is, a rule that tells you how to associate any value $a$ in an input set a unique value $b$ in an output set.

Example: Consider the math function $f:\mathbb R\rightarrow \mathbb R$ given by

$$f(x) = \cos(x) e^{-\frac{x^2}{5}}.$$

In [11]:
from math import exp, cos

In Python, one way to define a function is by using the following construct:

def function_name(arg1, arg2, arg3):
    instruction 1
    instruction 2
    instruction 3
    return z

The input of the function $f$ is the argements arg1, arg2, arg3, and the output of $f$ is whatever comes after the keyword return.

Remark: If not the function does not have a return instruction, then nothing is returned!!!!


In [27]:
def f(x):
    z = cos(x) * exp(-(x**2/5.0))
    return z

In [31]:
def g(x):
    return cos(x)**2 + exp(x)

Function call

A function call is an expression of the form

function_name(val1, val2, val3)

where now val1, val2, val3 are concrete values and not parameters. The interpreter evaluates a function call by replacing the function call by the function output.


In [24]:
z = g(6)
y = f(4)


404.350720472
-0.0266439546028

In [25]:
y

In [26]:
z

Function Compostion

In math, if you have two functions $f:A\rightarrow B$ and $g:B\rightarrow C$, you can compose these functions by appliying first $f$ to and element $a\in A$ and then the second function $g$ to the output $f(a)\in B$. The result is a new function called the composition: $g \circ f(a):= g(f(a))$ yielding the function $g\circ f:A \rightarrow C$.

In Python (and in any other programming languages), one composes function the same way:


In [32]:
f(g(g(f(3))))


Out[32]:
0.00040507051471082436

In [36]:
def noReturnFct(x,y):
    print x + y
    return 'My grandma'

In [37]:
z = noReturnFct(8,9)


17

In [38]:
z


Out[38]:
'My grandma'

In [ ]:

Main parameter and options

We can separate the parameter of function in two types:

  • main parameters, that are required for the function to work: it's the INPUT

  • optional parameter, that are not required, and that customize the function behaviour: they are the OPTIONS

Remark: Optional parameter are not required because they have default values

f(x, option1=value1, option2=value2, etc.)

If the function call is as follows:

f(4)

then, behind the scene, Python calls the function with the default arguments

f(4, value1, value2)

In [3]:
from math import exp

def f(x, mu=1):
    return mu**x * exp(mu * x)

The function above represent the family of functions

$$ f_\mu (x) = \mu^x\exp(\mu x),$$

where $x$ is the function input and $\mu$ is the family parameter.


In [4]:
f(4)


Out[4]:
54.598150033144236

In [5]:
f(4, mu=3)


Out[5]:
13183138.104939317

In [6]:
f(mu=3, 4)


  File "<ipython-input-6-a6e77dadcf33>", line 1
    f(mu=3, 4)
SyntaxError: non-keyword arg after keyword arg

In [7]:
f(mu=3, x=4)


Out[7]:
13183138.104939317