Until this point, we have been using functions and writing small simple functions that contain our main program. This week we will learn to write more powerful functions. Functions are one of the most powerful abstractions in computer science. Unfortunately for beginners, they can also be one of the most confusing, so ask questions.
Function basics
A function is a named block of code that acts like a small subprogram. The basic syntax for
defining a new function is shown below:
def <function name>( <parameters>):
"""
<descriptive comment>
"""
<body>
When using a function, you should treat a function as a black box that takes input, performs some well defined task with that input, and possibly returns some output. If you understand what the function is supposed to do, you do not need to understand how the function actually performs that task. Think about some functions that you have used already. What do they do? How do they work? You may not know the answer to the second question, and you do not need to know the answer. You should keep this point in mind when writing your functions. Users should not need to know how a function works to simply use the function.
Functons have a number of uses:
- Reduce code duplication
- Make programs easier to understand
- Make programs easier to maintain
- others?
A first function
Take a look at
speeding.py in your
inclasss/w05-functions folder. Run
update21 to get this file. Run the program. What does it do? How does it work? Where is the value of
speed in
computeFine initialized? Try to print the value of
fine in the
speeding function? What happens? Any ideas why?
Function terminology
- parameters: names of input variables in function, e.g., speed, and limit
- return value: A value that is returns by the function to the caller.
- calling the function: The point in which the function is actually used. In this case, speeding calls the function computeFine if the speed is over the limit.
- arguments: The actual input values passed by the caller to the function as input. your_speed and limit are examples.
- scope: variables defined in a function can only be used within that function. The variable fine is in the scope of computeFine. The variable your_speed is in the scope of speeding. What about the variable limit?
Tracing functions
Code containig functions is typically processed in a very non-linear way. When a function is called, the following steps take place:
- The calling function suspends execution at the point of the call.
- The values of the arguments from caller are copied to the parameters of the function in order.
- The body of the called function executes.
- The return value of the called function is returned to the point at which the function was called
- The calling function continues to execute the rest of its body.
Python keeps track of all of this using stack frames. You should too!
Writing functions
Let's take a look at
animate.py from last week. Begin by copying this file from your
w04-graphics folder to your
w05-functions folder.
$ cd
caraway[~]$ cd cs21
caraway[cs21]$ ls
examples/ inclass/ labs/
caraway[cs21]$ cd inclass/w05-functions/
caraway[w05-functions]$ ls
speeding.py
caraway[w05-functions]$ cp ../w04-graphics/animate.py .
caraway[w05-functions]$ ls
animate.py speeding.py
Modify the code such that the animation part calls a new function
animate. The function should take as input an object to move, an amount to move in in the x direction, an amount to move in the y direction, a number of steps and total time to move. The function should move the object from inside the function. It does not need to return anything.