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>
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:

A few first example
open 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

Tracing functions
Code containing 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 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 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 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 We are going to write some functions together.