Lab#7 Functions
Lab#7 Functions
Functions
A function
• Define a variable for each argument (sideLength). These variables are called the parameter
variables.
Put all this information together along with the def reserved word to form the first line of the
function’s definition:
def cubeVolume(sideLength) :
This line is called the header of the function. Next, specify the body of the function. The body
contains the statements that are executed when the function is called.
.
We will store this value in a variable called volume:
volume = sideLength ** 3
In order to return the result of the function, use the return statement:
return volume
Here is the complete function:
def cubeVolume(sideLength) :
volume = sideLength ** 3
return volume
Testing a function
In the preceding section, you saw how to write a function. If you run a program containing just
the function definition, then nothing happens. After all, nobody is calling the function.
result1 = cubeVolume(2)
result2 = cubeVolume(10)
Note that the function returns different results when it is called with different arguments.
Consider the call cubeVolume(2). The argument 2 corresponds to the sideLength parameter variable.
Therefore, in this call, sideLength is 2. The function computes sideLength ** 3, or 2 ** 3. When the
function is called with a different argument, say 10, then the function computes 10** 3.
Parameter Passing:
def area_circle(radius):
pi = 3.14159
area = pi * radius**2
return area
When you write a program that contains one or more functions, you need to pay attention to the
order of the function definitions and statements in the program.
Have another look at the program of the preceding section. Note that it contains
• The definition of the cubeVolume function.
• Several statements, two of which call that function.
As the Python interpreter reads the source code, it reads each function definition and each
statement. The statements in a function definition are not executed until the function is
called. Any statement not in a function definition, on the other hand, is executed as it is
encountered. Therefore, it is important that you define each function before you call it. For
example, the following will produce a compile-time error:
print(cubeVolume(10))
def cubeVolume(sideLength) :
volume = sideLength ** 3
return volume
The compiler does not know that the cubeVolume function will be defined later in the program.
However, a function can be called from within another function before the former has been
defined. For example, the following is perfectly legal:
def main() :
result = cubeVolume(2)
print("A cube with side length 2 has volume", result)
def cubeVolume(sideLength) :
volume = sideLength ** 3
return volume
main()
Note that the cubeVolume function is called from within the main function even though
cubeVolume is defined after main. To see why this is not a problem, consider the flow of
execution. The definitions of the main and cubeVolume functions are processed. The statement
in the last line is not contained in any function. Therefore, it is executed directly. It calls the main
function. The body of the main function executes, and it calls cubeVolume, which is now known.