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.
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>):
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
A few first example
in vim. We will look over the code which
calls to four functions, each is an example of functions that do or do
take input values and functions that do or do not return a value.
Once we think we understand what the program is doing, let's try running it.
- parameters: names of input variables in function, e.g., n, string, and letter
- 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, main calls the function get_name to get a user name.
- arguments: The actual input values passed by the caller to the function as input. name and num are examples when print_string is called from main.
- scope: variables defined in a function can only be used within that function. The variable count is in the scope of count_letters. The variable i is in the scope of print_string. What about the variable name?
Code containing 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!
We are going to do some of the following together in class:
- open factorial.py. We will look over the code together. This
is an example of a function that takes input (it has parameters and
the function call contains argument values for the parameters) and
an example of a function that returns a value to the caller.
- open squares.py. Together we are going to write
a function square_the_biggest that takes two input values and
returns the square of the larger of the two values. Let's make
sure to test it for different input values to be sure it is
- open squares_list.py. Together we are going to write
a different version of square_the_biggest. This version takes
a list of input values, and returns the square of the largest
value in the list.
- open draw_shapes.py. We are going to write some functions together.