4.1 Functions: How to Write Them?
83
4.1.4 Local Versus Global Variables
In our program ball_function.py, we have defined the variable g inside the
function y. This makes g a local variable, meaning that it is only known inside
the function. Thus, if we had tried to use g outside of the function, we would have
got an error message. To see this, we may insert print(v0*t - 0.5*g*t**2) as
a new last line in main (i.e., after the last print(y(v0, time))) and try to run
the code. Now, Python will not recognize g, even if Python just used it inside the
function y (you should try this to see for yourself, but remember to remove the
inserted code after the test!).
The variables v0 and time are defined outside the function and are therefore
global variables. They are known both outside and inside the function. 4
Input parameters listed in a function header are by rule local variables inside the
function. If you define one global and one local variable, both with the same name
(as v0 in ball_function.py), the function body only “sees” the local one, so the
global variable is not affected by what happens to its local “name-brother”.
If you want to change the value of a global variable inside a function, you need to
declare the variable as global inside the function. That is, if some global variable was
named x, we would need to write global x inside the function definition before we
let the function change it. After function execution, x would then have a changed
value.
4.1.5 Calling a Function Defined with Positional Parameters
We will here discuss alternative ways of calling the function y from
ballnrea_function.py. This function has only got positional parameters, and
throughout, the definition
def y(v0, t):
g = 9.81
return v0*t - 0.5*g*t**2
is kept unchanged.
Parameter Versus Argument As explained previously, the input variables specified in a function definition are called the parameters of the function. When we call
that function, however, the values provided in the call are referred to as arguments. 5
Mixing the Position of Arguments Assume now, like before, that we just want our
main program to compute the height of the ball after 0.6 s, when the initial velocity
4 The observant reader may then ask why not just define the function y without input parameters
v0 and t, and simply use v0 and time directly inside the function (changing t to time in the return
statement)? The answer is that this would have worked. However, there are several issues related
to the use of global variables, so it is something that should be avoided in general.
5 https://docs.python.org/3.3/faq/programming.html#faq-argument-vs-parameter.
83
4.1.4 Local Versus Global Variables
In our program ball_function.py, we have defined the variable g inside the
function y. This makes g a local variable, meaning that it is only known inside
the function. Thus, if we had tried to use g outside of the function, we would have
got an error message. To see this, we may insert print(v0*t - 0.5*g*t**2) as
a new last line in main (i.e., after the last print(y(v0, time))) and try to run
the code. Now, Python will not recognize g, even if Python just used it inside the
function y (you should try this to see for yourself, but remember to remove the
inserted code after the test!).
The variables v0 and time are defined outside the function and are therefore
global variables. They are known both outside and inside the function. 4
Input parameters listed in a function header are by rule local variables inside the
function. If you define one global and one local variable, both with the same name
(as v0 in ball_function.py), the function body only “sees” the local one, so the
global variable is not affected by what happens to its local “name-brother”.
If you want to change the value of a global variable inside a function, you need to
declare the variable as global inside the function. That is, if some global variable was
named x, we would need to write global x inside the function definition before we
let the function change it. After function execution, x would then have a changed
value.
4.1.5 Calling a Function Defined with Positional Parameters
We will here discuss alternative ways of calling the function y from
ballnrea_function.py. This function has only got positional parameters, and
throughout, the definition
def y(v0, t):
g = 9.81
return v0*t - 0.5*g*t**2
is kept unchanged.
Parameter Versus Argument As explained previously, the input variables specified in a function definition are called the parameters of the function. When we call
that function, however, the values provided in the call are referred to as arguments. 5
Mixing the Position of Arguments Assume now, like before, that we just want our
main program to compute the height of the ball after 0.6 s, when the initial velocity
4 The observant reader may then ask why not just define the function y without input parameters
v0 and t, and simply use v0 and time directly inside the function (changing t to time in the return
statement)? The answer is that this would have worked. However, there are several issues related
to the use of global variables, so it is something that should be avoided in general.
5 https://docs.python.org/3.3/faq/programming.html#faq-argument-vs-parameter.
