So far, we have only been using the functions that come with Python, but it is also possible to add new functions. A function definition specifies the name of a new function and the sequence of statements that run when the function is called.
Here is an example:
def is a keyword that indicates that this is a function
definition. The name of the function is print_lyrics
. The
rules for function names are the same as for variable names: letters,
numbers and underscore are legal, but the first character
can’t be a number. You can’t use a keyword as the name of a function,
and you can’t have a variable and a function with the same
name.
The empty parentheses after the name indicate that this function doesn’t take any arguments.
The first line of the function definition is called the header; the rest is called the body. The header has to end with a colon and the body has to be indented. By convention, indentation is always four spaces. The body can contain any number of statements.
Defining a function creates a function object, which
has type function
:
The syntax for calling the new function is the same as for built-in functions:
(Reminder: do not use the type
function except while debugging.)
Once you have defined a function, you can use it inside another
function. For example, to repeat the previous refrain, we could write
a function called repeat_lyrics
:
And then call repeat_lyrics
:
But that’s not really how the song goes.
Pulling together the code fragments from the previous section, the whole program looks like this:
This program contains two function definitions: print_lyrics
and
repeat_lyrics
. Function definitions get executed just like other
statements, but the effect is to create function objects. The statements
inside the function do not run until the function is called, and
the function definition generates no output.
As you might expect, you have to create a function before you can run it. In other words, the function definition has to run before the function gets called.
As an exercise, move the last line of this program to the top, so the function call appears before the definitions. Run the program and see what error message you get.
Now move the function call back to the bottom
and move the definition of print_lyrics
after the definition of
repeat_lyrics
. What happens when you run this program?
To ensure that a function is defined before its first use, you have to know the order statements run in, which is called the flow of execution.
Execution always begins at the first statement of the program. Statements are run one at a time, in order from top to bottom.
Function definitions do not alter the flow of execution of the program, but remember that statements inside the function don’t run until the function is called.
A function call is like a detour in the flow of execution. Instead of going to the next statement, the flow jumps to the body of the function, runs the statements there, and then comes back to pick up where it left off.
That sounds simple enough, until you remember that one function can call another. While in the middle of one function, the program might have to run the statements in another function. Then, while running that new function, the program might have to run yet another function!
Fortunately, Python is good at keeping track of where it is, so each time a function completes, the program picks up where it left off in the function that called it. When it gets to the end of the program, it terminates.
In summary, when you read a program, you don’t always want to read from top to bottom. Sometimes it makes more sense if you follow the flow of execution.
Some of the functions we have seen require arguments. For example, when you call math.sin you pass a number as an argument. Some functions take more than one argument: math.pow takes two, the base and the exponent.
Inside the function, the arguments are assigned to variables called parameters. Here is a definition for a function that takes an argument:
This function assigns the argument to a parameter named bruce. When the function is called, it prints the value of the parameter (whatever it is) twice.
This function works with any value that can be printed.
Just as we can call a built-in function using an expression as the argument,
so can we call our own functions.
So we can use any kind of expression
as an argument for print_twice
:
The argument is evaluated before the function is called, so
in the examples the expressions ’Spam ’*4
and
math.cos(math.pi) are only evaluated once.
You can also use a variable as an argument:
The name of the variable we pass as an argument (michael) has
nothing to do with the name of the parameter (bruce). It
doesn’t matter what the value was called back home (in the caller);
here in print_twice
, we call everybody bruce.