# Functions

Writing functions
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 few first example
open example_funcs.py in vim. We will look over the code which contains calls to four functions, each is an example of functions that do or do not 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.

Function terminology

• 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?
Tracing functions
Code containig functions is typically processed in a very non-linear way. When a function is called, the following steps take place:
1. The calling function suspends execution at the point of the call.
2. The values of the arguments from caller are copied to the parameters of the function in order.
3. The body of the called function executes.
4. The return value of the called function is returned to the point at which the function was called
5. 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
We are going to do some of the following together in class:
1. 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.
2. 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 correct.
3. 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.
4. open draw_shapes.py. We are going to write some functions together.