80
4 Functions and the Writing of Code
return v0*t - 0.5*g*t**2
v0 = 5
# Initial velocity
time = 0.6
# Just pick one point in time
print(y(v0, time))
time = 0.9
# Pick another point in time
print(y(v0, time))
The Function Definition When Python reads this program from the top, it takes
the code from the line with def, to the line with return, to be the definition of a
function by the name y. Note that this function (or any function for that matter) is
not executed until it is called. Here, that happens with the two calls (y(v0, time))
appearing in the print commands further down in the code. In other words, when
Python reads the function definition for the first time, it just “stores the definition
for later use” (and checks for syntax errors, see Exercise 4.8).
Calling the Function When the function is called the first time, the values of v0 (5)
and time (0.6) are transferred to the y function such that, in the function, v0 = 5
and t = 0.6. Thereafter, Python executes the function line by line. In the final line
return v0*t - 0.5*g*t**2, the expression v0*t - 0.5*g*t**2 is computed,
resulting in a number which is “returned” to replace y(v0, time) in the calling
code. The function print is then called with this number as input argument and
proceeds to print it (i.e., the height). Alternatively, the number returned from y could
have been assigned to a variable, e.g., like h = y(v0, time), before printing as
print(h).
Python then proceeds with the next line, setting time to 0.9, before a new
function call is triggered in the following line, causing a second execution of the
function y and printout of the new height.
Variable Names in Function Calls Observe that, when calling the function y, the
time was contained in the variable time, whereas the corresponding input variable
(called a parameter) in the function y had the name t. We should reveal already
now, that in general, variable names in function calls do not have to be the same
as the corresponding names in the function definition. However, with v0 here,
we see that variable names can be the same in the function call and the function
definition (but technically, they are then two different variables with the same name,
see Sect. 4.1.4). More rules about this will follow soon.
4.1.2 Characteristics of a Function Definition
Function Structure We may now write up a more general form of a Python
function as
def function_name(p1, p2, p3, ...):
# function header
"""This is a docstring"""
# ...in function body
4 Functions and the Writing of Code
return v0*t - 0.5*g*t**2
v0 = 5
# Initial velocity
time = 0.6
# Just pick one point in time
print(y(v0, time))
time = 0.9
# Pick another point in time
print(y(v0, time))
The Function Definition When Python reads this program from the top, it takes
the code from the line with def, to the line with return, to be the definition of a
function by the name y. Note that this function (or any function for that matter) is
not executed until it is called. Here, that happens with the two calls (y(v0, time))
appearing in the print commands further down in the code. In other words, when
Python reads the function definition for the first time, it just “stores the definition
for later use” (and checks for syntax errors, see Exercise 4.8).
Calling the Function When the function is called the first time, the values of v0 (5)
and time (0.6) are transferred to the y function such that, in the function, v0 = 5
and t = 0.6. Thereafter, Python executes the function line by line. In the final line
return v0*t - 0.5*g*t**2, the expression v0*t - 0.5*g*t**2 is computed,
resulting in a number which is “returned” to replace y(v0, time) in the calling
code. The function print is then called with this number as input argument and
proceeds to print it (i.e., the height). Alternatively, the number returned from y could
have been assigned to a variable, e.g., like h = y(v0, time), before printing as
print(h).
Python then proceeds with the next line, setting time to 0.9, before a new
function call is triggered in the following line, causing a second execution of the
function y and printout of the new height.
Variable Names in Function Calls Observe that, when calling the function y, the
time was contained in the variable time, whereas the corresponding input variable
(called a parameter) in the function y had the name t. We should reveal already
now, that in general, variable names in function calls do not have to be the same
as the corresponding names in the function definition. However, with v0 here,
we see that variable names can be the same in the function call and the function
definition (but technically, they are then two different variables with the same name,
see Sect. 4.1.4). More rules about this will follow soon.
4.1.2 Characteristics of a Function Definition
Function Structure We may now write up a more general form of a Python
function as
def function_name(p1, p2, p3, ...):
# function header
"""This is a docstring"""
# ...in function body
# ...in function body
# ...in function body 