User Guide
Table Of Contents
143
To specify the arguments in a different order, you can use
keyword arguments. As shown below, keyword arguments
specify the parameter name before the argument.
myFunction2(val2=y, val1=x)
It is important to realize that when you change an argument
inside a function, the value of it also changes outside of the
function. This is because Python actually passes a reference to
the argument, not a copy of it.
For example:
def Inc(val):
val = val + 1
x = 1
print(x)
Inc(x)
print(x)
Making Parameters Optional
Sometimes it can be useful to call a function without including all
of the arguments. You can do this by specifying the default value
that a parameter should have if an argument is not passed into
the function.
def PrintAndMultiply(val1, val2 = 1):
print(val1 * val2)
You can call this function with PrintAndMultiply(5,2) to see the
value 10, or just PrintAndMultiply(5). If you do not pass the
second argument, then Python assigns the value 1 to val2
because that is the default specified in the function’s definition.
An argument is a variable or value that you pass into a function. In
the examples above, x and y are arguments. Parameters are part
of the function’s definition and refer to the names given to the
data when it is passed to the function. For example, val1 and val2
are parameters.