4.1 Functions: How to Write Them?
81
...
...
return result_1, result_2, ...
# last line in function body
# First line after function definition
A function definition must appear before the function is called, and usually, function
definitions are placed one after the other at the top of a program, after import
statements (if any).
Function Header (Positional Parameters) The first line, often called the function
header, always starts with the reserved word def. This word is succeeded by
the name of the function, followed by a listing of comma-separated names in
parentheses, being the parameters of the function (here symbolized with p1, p2,
p3,. . . ). These parameters specify what input the function needs for execution. The
header always ends with a colon. Function and parameter names must be decided
by the programmer.
The function header may contain any number 1 of parameters, also none.
When there are no parameters, the parentheses must still be there, i.e., like
def function_name():.
When input parameters are listed on the form above, they are referred to as
positional parameters. Below, in Sect. 4.1.5, we will see what freedom there is when
calling functions defined with such parameters.
Another Typical Function Header (Keyword Parameters) Another kind of
function header that you often will deal with, is one which allows default values
to be given to some, or all, of the input parameters. We may write such a header on
the following form 2 (it could replace the previous header that has only positional
parameters):
def function_name(p1, p2, p3=default_3, p4=default_4, ...):
In this case, the two first parameters are positional, whereas p3 and p4 are known
as keyword parameters. This means that, unless other values are specified for p3
and p4 when calling the function, they will get the default values default_3
and default_4, respectively. This will soon (Sect. 4.1.7) be demonstrated and
discussed in more detail through examples.
A function header may have only positional parameters, only keyword parameters, or some of each. However, positional parameters must always be listed before
keyword parameters (as shown in the header above).
Note that there should be no space to each side of = when specifying keyword
parameters.
Function Body All code lines inside a function must be indented, conventionally
with 4 spaces, or more (if there are more indentation levels). In a function, these
indented lines represent a block of statements, collectively referred to as the
function body. Once the indent is reversed, we are outside (and after) the function.
1 It is possible to have a variable number of input parameters (using *args and **kwargs).
However, we do not pursue that any further here.
2 Strictly speaking, this header also incorporates the previous header, but they were split just to
clarify the presentation.
81
...
...
return result_1, result_2, ...
# last line in function body
# First line after function definition
A function definition must appear before the function is called, and usually, function
definitions are placed one after the other at the top of a program, after import
statements (if any).
Function Header (Positional Parameters) The first line, often called the function
header, always starts with the reserved word def. This word is succeeded by
the name of the function, followed by a listing of comma-separated names in
parentheses, being the parameters of the function (here symbolized with p1, p2,
p3,. . . ). These parameters specify what input the function needs for execution. The
header always ends with a colon. Function and parameter names must be decided
by the programmer.
The function header may contain any number 1 of parameters, also none.
When there are no parameters, the parentheses must still be there, i.e., like
def function_name():.
When input parameters are listed on the form above, they are referred to as
positional parameters. Below, in Sect. 4.1.5, we will see what freedom there is when
calling functions defined with such parameters.
Another Typical Function Header (Keyword Parameters) Another kind of
function header that you often will deal with, is one which allows default values
to be given to some, or all, of the input parameters. We may write such a header on
the following form 2 (it could replace the previous header that has only positional
parameters):
def function_name(p1, p2, p3=default_3, p4=default_4, ...):
In this case, the two first parameters are positional, whereas p3 and p4 are known
as keyword parameters. This means that, unless other values are specified for p3
and p4 when calling the function, they will get the default values default_3
and default_4, respectively. This will soon (Sect. 4.1.7) be demonstrated and
discussed in more detail through examples.
A function header may have only positional parameters, only keyword parameters, or some of each. However, positional parameters must always be listed before
keyword parameters (as shown in the header above).
Note that there should be no space to each side of = when specifying keyword
parameters.
Function Body All code lines inside a function must be indented, conventionally
with 4 spaces, or more (if there are more indentation levels). In a function, these
indented lines represent a block of statements, collectively referred to as the
function body. Once the indent is reversed, we are outside (and after) the function.
1 It is possible to have a variable number of input parameters (using *args and **kwargs).
However, we do not pursue that any further here.
2 Strictly speaking, this header also incorporates the previous header, but they were split just to
clarify the presentation.
