4.1 Functions: How to Write Them?
85
and test it!). This is because Python reads the listed arguments from left to right, and
does not know that an argument is taken out of the list before it comes to the end
(but then it is too late).
4.1.6 A Function with Two Return Values
Take a slight variation of our case with the ball, assuming that the ball is not thrown
straight up, but at an angle, so that two coordinates are needed to specify its position
at any time.
According to Newton’s laws (when air resistance is negligible), the vertical
position is given by y(t) = v 0y t − 0.5gt 2 and, simultaneously, the horizontal
position by x(t) = v 0x t. We can include both these expressions in a new version of
our program that finds the position of the ball at a given time:
def xy(v0x, v0y, t):
"""Compute horizontal and vertical positions at time t"""
g = 9.81
# acceleration of gravity
return v0x*t, v0y*t - 0.5*g*t**2
v_init_x = 2.0
# initial velocity in x
v_init_y = 5.0
# initial velocity in y
time = 0.6
# chosen point in time
x, y = xy(v_init_x, v_init_y, time)
print(’Horizontal position: {:g} , Vertical position: {:g}’.format(x, y))
We note that in the return statement, the returned values (two here) are separated
by a comma. Here, the x coordinate will be computed by v0x*t, while the y
coordinate will be computed like before. These two calculations produce two
numbers that are returned to variables x and y in main. Note that, the order of the
results returned from the function must be the same as the order of the corresponding
variables in the assignment statement. Note the use of a comma both in the return
statement and the assignment statement.
4.1.7 Calling a Function Defined with Keyword Parameters
Let us adjust the previous program slightly, introducing keyword parameters in the
definition. For example, if we use 0.6 as a default value for t, and aim to get the
same printout as before, the program reads
def xy(v0x, v0y, t=0.6):
"""Compute horizontal and vertical positions at time t"""
g = 9.81
# acceleration of gravity
return v0x*t, v0y*t - 0.5*g*t**2
v_init_x = 2.0
# initial velocity in x
v_init_y = 5.0
# initial velocity in y
x, y = xy(v_init_x, v_init_y)
print(’Horizontal position: {:g} , Vertical position: {:g}’.format(x, y))
85
and test it!). This is because Python reads the listed arguments from left to right, and
does not know that an argument is taken out of the list before it comes to the end
(but then it is too late).
4.1.6 A Function with Two Return Values
Take a slight variation of our case with the ball, assuming that the ball is not thrown
straight up, but at an angle, so that two coordinates are needed to specify its position
at any time.
According to Newton’s laws (when air resistance is negligible), the vertical
position is given by y(t) = v 0y t − 0.5gt 2 and, simultaneously, the horizontal
position by x(t) = v 0x t. We can include both these expressions in a new version of
our program that finds the position of the ball at a given time:
def xy(v0x, v0y, t):
"""Compute horizontal and vertical positions at time t"""
g = 9.81
# acceleration of gravity
return v0x*t, v0y*t - 0.5*g*t**2
v_init_x = 2.0
# initial velocity in x
v_init_y = 5.0
# initial velocity in y
time = 0.6
# chosen point in time
x, y = xy(v_init_x, v_init_y, time)
print(’Horizontal position: {:g} , Vertical position: {:g}’.format(x, y))
We note that in the return statement, the returned values (two here) are separated
by a comma. Here, the x coordinate will be computed by v0x*t, while the y
coordinate will be computed like before. These two calculations produce two
numbers that are returned to variables x and y in main. Note that, the order of the
results returned from the function must be the same as the order of the corresponding
variables in the assignment statement. Note the use of a comma both in the return
statement and the assignment statement.
4.1.7 Calling a Function Defined with Keyword Parameters
Let us adjust the previous program slightly, introducing keyword parameters in the
definition. For example, if we use 0.6 as a default value for t, and aim to get the
same printout as before, the program reads
def xy(v0x, v0y, t=0.6):
"""Compute horizontal and vertical positions at time t"""
g = 9.81
# acceleration of gravity
return v0x*t, v0y*t - 0.5*g*t**2
v_init_x = 2.0
# initial velocity in x
v_init_y = 5.0
# initial velocity in y
x, y = xy(v_init_x, v_init_y)
print(’Horizontal position: {:g} , Vertical position: {:g}’.format(x, y))
