2.16. Functions

Functions in Python follow rules and syntax similar to most other languages: Functions are called using the functional operator ( ( ) ), functions must be declared before they are used, and the function type is the type of the value returned.

All arguments of function calls are made by reference, meaning that any changes to these parameters within the function affect the original objects in the calling function.

How to Declare a Function

							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 which the function may take. Function arguments such as arguments above are optional, hence the reason 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 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. Observe how the + operator works with non-numeric types.

Default arguments

Functions may have arguments which 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=1):
…       'determine if in debug mode with default argument'
…       if debug:
…           print 'in debug mode'
…       print 'done'
…
>>> foo()
in debug mode
done
>>> foo(0)
done

In the example above, the debug parameter has a default value of 1. When we do not pass in an argument to the function foo(), debug automatically takes on a true value of 1. On our second call to foo(), we deliberately send an argument of 0, 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset