You've also seen functions in math. For example:
$f(x) = 2x + 7$
This is the definition of $f(x)$. By itself it doesn't have a value. It just defines what it would mean to call the function $f$ on some value.
Given a value for $x$, we can calculate the resulting value of $f(x)$. $f(2)$ evaluates to 11, $f(-1$) gives 5.
In Python, in addition to using the built-in functions, we can define our own. For example, we can define a function equivalent to $f(x) = 2x + 7$:
def f(x): return 2 * x + 7
Here is the general form of a function definition:
return statement has this form:
return statement is executed as follows:
Once a function has been defined, we can call on the function. A function call has the form:
A function call is executed as follows:
Revisiting our earlier example, the function definition was:
def f(x): return 2 * x + 7
Answer the following questions:
f(8), what is the argument?
result = f(4). After that statement is executed, what value does
When defining functions, we will follow this Function Design Recipe:
Let's write a function that squares a number.
Step 1: Give examples of how we will call the function:
>>> square(5) 25 >>> square(-6) 36
square isn't defined yet, so we can't actually call it. Instead just work out, what the function calls should produce.
Step 2: Write the header, which is the line that starts with
def. The function will have one parameter, which we named
num, but we could have named it
n, or even
banana. Of course, it is best to pick a meaningful name.
While you are at it, make the examples that you wrote be part of a triple-quoted string inside the function (make sure to indent them 4 spaces). This triple-quoted string is called a docstring, which stands for documentation string.
def square(num): """ >>> square(5) 25 >>> square(-6) 36 """
Step 3: Add a type contract. Our function takes a single parameter which is a number. It returns a number. So the type contract is (number) -> number. Let's add that to our function.
def square(num): """ (number) -> number >>> square(5) 25 >>> square(-6) 36 """
Step 4: Write the description and it to the doctstring.
def square(num): """(number) -> number Return num squared. >>> square(5) 25 >>> my_abs(-6) 36 """
Step 5: Code the body. For this step, we write the Python statements that will be executed when the function is called. We will use a
return statement to produce a value.
def square(num): """(number) -> number Return num squared. >>> square(5) 25 >>> my_abs(-6) 36 """ return num * num
Step 6: Test our function. For now, we will do this by calling on the examples we developed in Step 1.
Generally in Canada, people know their weight in pounds. Most of the calculations for determining dosages and other medical information, ask for a patient's weight in kilograms. Following the Function Design Recipe, write a function that will convert from pounds to kilograms. (1kg == 2.2 lbs)
You will use a tool called PCRS to complete this week's homework exercise. Login with your UTORid and solve the problem in Phase I: [Your level] Session 1 Practice Exercise. Our instance of PCRS is available here: https://pcrs.teach.cs.toronto.edu/c4m_sep2016