4
Functions and the Writing of Code
4.1 Functions: How to Write Them?
Until now, we have benefitted from using functions like, e.g., zeros and linspace
from numpy. These have all been written by others for us to use. Now we will
look at how to write such functions ourselves, an absolutely fundamental skill in
programming.
In its simplest form, a function in a program is much like a mathematical
function: some input number x is transformed to some output number. One example
is the tanh
−1 (x) function, called atan in computer code: it takes one real number
as input and returns another number. Functions in Python are more general and can
take a series of values as input and return one or more results, or simply nothing.
The purpose of functions is twofold:
1. to group code lines that naturally belong together (making such units of code is
a strategy which may ease the problem solving process dramatically), and
2. to parameterize a set of code lines such that they can be written only once and
easily be re-executed with variations.
Functions that we write ourselves are often referred to as user-defined functions.
Throughout this book, we will present many examples, in various contexts, of how
such functions may be written.
4.1.1 Example: Writing Our First Function
To grasp the first few essentials about function writing, we change ball.py from
Sect. 1.2, so that the height y is rather computed by use of a function that we define
ourselves. Also, to better demonstrate the use of our function, we compute y at two
points in time (not only one, as in ball.py). The new program ball_function.py
then appears as
def y(v0, t):
g = 9.81
# Acceleration of gravity
© The Author(s) 2020
S. Linge, H. P. Langtangen, Programming for Computations - Python,
Texts in Computational Science and Engineering 15,
https://doi.org/10.1007/978-3-030-16877-3_4
79
Functions and the Writing of Code
4.1 Functions: How to Write Them?
Until now, we have benefitted from using functions like, e.g., zeros and linspace
from numpy. These have all been written by others for us to use. Now we will
look at how to write such functions ourselves, an absolutely fundamental skill in
programming.
In its simplest form, a function in a program is much like a mathematical
function: some input number x is transformed to some output number. One example
is the tanh
−1 (x) function, called atan in computer code: it takes one real number
as input and returns another number. Functions in Python are more general and can
take a series of values as input and return one or more results, or simply nothing.
The purpose of functions is twofold:
1. to group code lines that naturally belong together (making such units of code is
a strategy which may ease the problem solving process dramatically), and
2. to parameterize a set of code lines such that they can be written only once and
easily be re-executed with variations.
Functions that we write ourselves are often referred to as user-defined functions.
Throughout this book, we will present many examples, in various contexts, of how
such functions may be written.
4.1.1 Example: Writing Our First Function
To grasp the first few essentials about function writing, we change ball.py from
Sect. 1.2, so that the height y is rather computed by use of a function that we define
ourselves. Also, to better demonstrate the use of our function, we compute y at two
points in time (not only one, as in ball.py). The new program ball_function.py
then appears as
def y(v0, t):
g = 9.81
# Acceleration of gravity
© The Author(s) 2020
S. Linge, H. P. Langtangen, Programming for Computations - Python,
Texts in Computational Science and Engineering 15,
https://doi.org/10.1007/978-3-030-16877-3_4
79
