Home > Articles > Open Source > Python

  • Print
  • + Share This
This chapter is from the book

2.17 Functions

Like many other languages, functions in Python are called using the functional operator ( ( ) ), functions must be declared before they can be called. You do not need to declare function (return) types or explicitly return values (None, Python’s NULL object is returned by default if one is not given.)

Python can be considered “call by reference.” This means that any changes to these parameters within the function affect the original objects in the calling function. However, the caveat is that in Python, it is really dependent on the object type being passed. If that object allows updating, then it behaves as you would expect from “call by reference,” but if that object’s value cannot be changed, then it will behave like “call by value.”

How to Declare Functions

def function_name([arguments]):
 "optional documentation string"
 function_suite

The syntax for declaring a function consists of the def keyword followed by the function name and any arguments that the function may take. Function arguments such as arguments above are optional, which is why they are enclosed in brackets above. (Do not physically put brackets in your code!) The statement terminates with a colon (the same way that an if or while statement is terminated), and a code suite representing the function body follows. Here is one short example:

def addMe2Me(x):
 'apply + operation to argument'
 return (x + x)

This function, presumably meaning “add me to me” takes an object, adds its current value to itself and returns the sum. While the results are fairly obvious with numerical arguments, we point out that the plus sign works for almost all types. In other words, most of the standard types support the + operator, whether it be numeric addition or sequence concatenation.

How to Call Functions

>>> addMe2Me(4.25)
8.5
>>>
>>> addMe2Me(10)
20
>>>
>>> addMe2Me('Python')
'PythonPython'
>>>
>>> addMe2Me([-1, 'abc'])
[-1, 'abc', -1, 'abc']

Calling functions in Python is similar to function invocations in many other high-level languages, by giving the name of the function followed by the functional operator, a pair of parentheses. Any optional parameters go between the parentheses, which are required even if there are no arguments. Observe how the +operator works with non-numeric types.

Default Arguments

Functions may have arguments that have default values. If present, arguments will take on the appearance of assignment in the function declaration, but in actuality, it is just the syntax for default arguments and indicates that if a value is not provided for the parameter, it will take on the assigned value as a default.

>>> def foo(debug=True):
...     'determine if in debug mode with default argument'
...     if debug:
...         print 'in debug mode'
...     print 'done'
...
>>> foo()
in debug mode
done
>>> foo(False)
done

In the example above, the debug parameter has a default value of True. When we do not pass in an argument to the function foo(), debug automatically takes on a value of True. On our second call to foo(), we deliberately send an argument of False, so that the default argument is not used.

Functions have many more features than we could describe in this introductory section. Please refer to Chapter 11 for more details.

  • + Share This
  • 🔖 Save To Your Account