82
4 Functions and the Writing of Code
Here, the comment # First line after function definition is after the
function.
The first line in the function body shown here, is an optional documentation
string, a docstring. This string is meant for a human interpreter, so it should say
something about the purpose with the function and explain input parameters and
return values, unless obvious. By convention, a docstring is enclosed in triple
double quotes ("""), which allows the string to run over several lines. When
present, the docstring must appear immediately after the function header, as shown
above.
The code lines of a function are executed only when the function is called (or
invoked), as explained above with ball_function.py. Usually, a function ends
with a return statement, starting with the reserved word return, which “returns”
one or more results back to the place where the function was called. However, the
explicit return statement may be dropped if not needed. 3 How to receive multiple
return values from a function call will be exemplified in Sect. 4.1.6.
A function body can have loops, branching and calls to other functions, and may
contain as many code lines as you need. Sometimes, a function body contains nothing more than a return statement of the form return,
where some calculation is carried out before returning the result. Note that function
input parameters are not required to be numbers. Any object will do, e.g., strings or
other functions.
4.1.3 Functions and the Main Program
An expression you will often encounter in programming, is main program, or that
some code is “in main”. This is nothing particular to Python, and simply refers to
that part of the program which is outside functions, taking function headers as part
of the main program.
We may exemplify this with ball_function.py from above, using comments
to tell which lines are not “in main”.
def y(v0, t):
g = 9.81
# not in main
return v0*t - 0.5*g*t**2
# not in main
v0 = 5
time = 0.6
print(y(v0, time))
time = 0.9
print(y(v0, time))
Thus, everything is in main except the two lines of the function body.
3 In that case, something called None is returned, being a certain object that represents “nothing”.
4 Functions and the Writing of Code
Here, the comment # First line after function definition is after the
function.
The first line in the function body shown here, is an optional documentation
string, a docstring. This string is meant for a human interpreter, so it should say
something about the purpose with the function and explain input parameters and
return values, unless obvious. By convention, a docstring is enclosed in triple
double quotes ("""), which allows the string to run over several lines. When
present, the docstring must appear immediately after the function header, as shown
above.
The code lines of a function are executed only when the function is called (or
invoked), as explained above with ball_function.py. Usually, a function ends
with a return statement, starting with the reserved word return, which “returns”
one or more results back to the place where the function was called. However, the
explicit return statement may be dropped if not needed. 3 How to receive multiple
return values from a function call will be exemplified in Sect. 4.1.6.
A function body can have loops, branching and calls to other functions, and may
contain as many code lines as you need. Sometimes, a function body contains nothing more than a return statement of the form return
where some calculation is carried out before returning the result. Note that function
input parameters are not required to be numbers. Any object will do, e.g., strings or
other functions.
4.1.3 Functions and the Main Program
An expression you will often encounter in programming, is main program, or that
some code is “in main”. This is nothing particular to Python, and simply refers to
that part of the program which is outside functions, taking function headers as part
of the main program.
We may exemplify this with ball_function.py from above, using comments
to tell which lines are not “in main”.
def y(v0, t):
g = 9.81
# not in main
return v0*t - 0.5*g*t**2
# not in main
v0 = 5
time = 0.6
print(y(v0, time))
time = 0.9
print(y(v0, time))
Thus, everything is in main except the two lines of the function body.
3 In that case, something called None is returned, being a certain object that represents “nothing”.
