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 [ ]:


In [ ]:

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 [ ]:


In [ ]:

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 [ ]:

Return values


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 [ ]:

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 [ ]:


In [ ]:


In [ ]:


In [ ]: