Pages

Wednesday, March 3, 2010

Default Argument Values


For some functions, you may want to make some of its parameters as optional and use default values if
the user does not want to provide values for such parameters. This is done with the help of default argument
values. You can specify default argument values for parameters by following the parameter name
in the function definition with the assignment operator (=) followed by the default value.
Note that the default argument value should be a constant. More precisely, the default argument value
should be immutable - this is explained in detail in later chapters. For now, just remember this.
Using Default Argument Values

Example 7.5. Using Default Argument Values
#!/usr/bin/python
# Filename: func_default.py
def say(message, times = 1):
print message * times
say('Hello')
say('World', 5)
Output
$ python func_default.py
Hello
WorldWorldWorldWorldWorld
How It Works
The function named say is used to print a string as many times as want. If we don't supply a value, then
by default, the string is printed just once. We achieve this by specifying a default argument value of 1 to
the parameter times.
In the first usage of say, we supply only the string and it prints the string once. In the second usage of
say, we supply both the string and an argument 5 stating that we want to say the string message 5
times.
Important
Only those parameters which are at the end of the parameter list can be given default argument
values i.e. you cannot have a parameter with a default argument value before a parameter
without a default argument value in the order of parameters declared in the function parameter
list.
This is because the values are assigned to the parameters by position. For example, def
func(a, b=5) is valid, but def func(a=5, b) is not valid.

Using the global statement


If you want to assign a value to a name defined outside the function, then you have to tell Python that
the name is not local, but it is global. We do this using the global statement. It is impossible to assign
a value to a variable defined outside a function without the global statement.
You can use the values of such variables defined outside the function (assuming there is no variable with
the same name within the function). However, this is not encouraged and should be avoided since it becomes
unclear to the reader of the program as to where that variable's definition is. Using the global statement makes it amply clear that the variable is defined in an outer block.
Example 7.4. Using the global statement
#!/usr/bin/python
# Filename: func_global.py
def func():
global x
print 'x is', x
x = 2
print 'Changed global x to', x
x = 50
func()
print 'Value of x is', x
Output
$ python func_global.py
x is 50
Changed global x to 2
Value of x is 2
How It Works
The global statement is used to decare that x is a global variable - hence, when we assign a value to x
inside the function, that change is reflected when we use the value of x in the main block.
You can specify more than one global variable using the same global statement. For example, global
x, y, z.

Local Variables

When you declare variables inside a function definition, they are not related in any way to other variables with the same names used outside the function i.e. variable names are local to the function. This is
called the scope of the variable. All variables have the scope of the block they are declared in starting
from the point of definition of the name.
Using Local Variables
Example 7.3. Using Local Variables
#!/usr/bin/python
# Filename: func_local.py
def func(x):
print 'x is', x
x = 2
print 'Changed local x to', x
x = 50
func(x)
print 'x is still', x
Output
$ python func_local.py
x is 50
Changed local x to 2
x is still 50
How It Works
In the function, the first time that we use the value of the name x, Python uses the value of the parameter
declared in the function.
Next, we assign the value 2 to x. The name x is local to our function. So, when we change the value of
x in the function, the x defined in the main block remains unaffected.
In the last print statement, we confirm that the value of x in the main block is actually unaffected.

Function Parameters


A function can take parameters which are just values you supply to the function so that the function can
do something utilising those values. These parameters are just like variables except that the values of

these variables are defined when we call the function and are not assigned values within the function itself.
Parameters are specified within the pair of parentheses in the function definition, separated by commas.
When we call the function, we supply the values in the same way. Note the terminology used - the
names given in the function definition are called parameters whereas the values you supply in the function
call are called arguments.
Using Function Parameters
Example 7.2. Using Function Parameters
#!/usr/bin/python
# Filename: func_param.py
def printMax(a, b):
if a > b:
print a, 'is maximum'
else:
print b, 'is maximum'
printMax(3, 4) # directly give literal values
x = 5
y = 7
printMax(x, y) # give variables as arguments
Output
$ python func_param.py
4 is maximum
7 is maximum
How It Works
Here, we define a function called printMax where we take two parameters called a and b. We find
out the greater number using a simple if..else statement and then print the bigger number.
In the first usage of printMax, we directly supply the numbers i.e. arguments. In the second usage, we
call the function using variables. printMax(x, y) causes value of argument x to be assigned to
parameter a and the value of argument y assigned to parameter b. The printMax function works the
same in both the cases.

Defining a Function


Example 7.1. Defining a function
#!/usr/bin/python
# Filename: function1.py
def sayHello():
print 'Hello World!' # block belonging to the function
# End of function
sayHello() # call the function
Output
$ python function1.py
Hello World!
How It Works
We define a function called sayHello using the syntax as explained above. This function takes no
parameters and hence there are no variables declared in the parentheses. Parameters to functions are just
input to the function so that we can pass in different values to it and get back corresponding results.