Game Development Reference
In-Depth Information
Parameters are local variables that get defined when a function is called. The value
stored in the parameter is the argument that was passed in the function call.
Parameters
For example, here is a short program that demonstrates parameters. Imagine we had a
short program that looked like this:
def sayHello(name):
print('Hello, ' + name)
print('Say hello to Alice.')
fizzy = 'Alice'
sayHello(fizzy)
print('Do not forget to say hello to Bob.')
sayHello('Bob')
If we run this program, it would look like this:
Say hello to Alice.
Hello, Alice
Do not forget to say hello to Bob.
Hello, Bob
This program calls a function we have created, sayHello() and first passes the value
in the fizzy variable as an argument to it. (We stored the string 'Alice' in fizzy .)
Later, the program calls the sayHello() function again, passing the string 'Bob' as an
argument.
The value in the fizzy variable and the string 'Bob' are arguments. The name
variable in sayHello() is a parameter. The difference between arguments and
parameters is that arguments are the values passed in a function call, and parameters are the
local variables that store the arguments. It might be easier to just remember that the thing in
between the parentheses in the def statement is an parameter, and the thing in between the
parentheses in the function call is an argument.
We could have just used the fizzy variable inside the sayHello() function instead
of using a parameter. (This is because the local scope can still see variables in the global
scope.) But then we would have to remember to assign the fizzy variable a string each
time before we call the sayHello() function. Parameters make our programs simpler.
Look at this code:
def sayHello():
Search WWH ::




Custom Search