- 2.1 Program Output, the print Statement, and Hello World!
- 2.2 Program Input and the raw_input()Built-in Function
- 2.3 Comments
- 2.4 Operators
- 2.5 Variables and Assignment
- 2.6 Numbers
- 2.7 Strings
- 2.8 Lists and Tuples
- 2.9 Dictionaries
- 2.10 Code Blocks Use Indentation
- 2.11 if Statement
- 2.12 while Loop
- 2.13 for Loop and the range() Built-in Function
- 2.14 List Comprehensions
- 2.15 Files and the open() and file() Built-in Functions
- 2.16 Errors and Exceptions
- 2.17 Functions
- 2.18 Classes
- 2.19 Modules
- 2.20 Useful Functions
- 2.21 Exercises
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.