# Lab: Functions¶

This lab will help you get practice using functions. It will also help you understand how functions are specified. From now on most of the things that I will have you write will be in a function. It’s important that you structure your function to match the question exactly.

Here’s an example of a function specification. It tells you the name of the function, what it should return the names of the arguments (if any) and their order. Though the argument names can change everything else should be the same.

• Name: the_function

• Arguments:

• arg1 (string) - The first argument

• arg2 (integer) - The second argument

• Return Values:

Here’s how you setup the answer:

def the_function(arg1, arg2):
"""This is the docstring for the_function"""
return "Mike"


Before you turn in or test your function make sure that:

1. You used the correct name

2. There is a docstring

3. You have the same number of arguments (you can rename them if you like)

4. You return the requested value. If the requested value is None you can simply return

## Part 1: Writing Functions¶

### 1. Write foo¶

Write a function called foo that takes one argument bar. The function should print the contents of bar.

• Name: foo

• Arguments:

• bar (string) - The thing to print

• Returns: None

[ ]:




Test your function using the cell below:

[ ]:




### 2. Write do_sum¶

Write a function called do_sum that takes two arguments, a and b. The function should print the sum of its arguments.

• Name: do_sum

• Arguments:

• a (float) - A number

• b (float) - Another number

• Returns: None

[ ]:




Test your function in the cell below:

[ ]:




### 3. Print Repeated¶

Write a function called print_repeat that takes two arguments, words and times. The function prints words times times.

• Name: print_repeat

• Arguments:

• words (string) - The words to repeat

• times (integer) - Another number

• Returns: None

[ ]:




Test your function is the cell below:

[ ]:




### 4. A Return Value¶

Write a function called i_am_four that returns the number 4.

• Name: i_am_four

• Arguments: None

• Returns: 4

[ ]:




Test your function in the cell below:

[ ]:




### 5. Arguments and Return Values¶

Write a function called do_sum_return that takes two arguments, a and b. The function returns the sum of the two numbers.

• Name: do_sum_return

• Arguments:

• a (float) A number

• b (float) Another number

• Returns: The sum of a and b

[ ]:




Test your function in the cell below:

[ ]:




### 6. Returning a Boolean Value¶

Write a function called is_greater that takes two arguments called a and b. The function returns True if a is greater than b, False otherwise.

• Name: is_greater

• Arguments:

• a (float) A number

• b (float) A number

• Returns:

• True if a is greater than b. False otherwise.

[ ]:




Test your function in the cell below:

[ ]:




## Part 2: Using Your Own Functions¶

In this part you’ll write reusable functions and use them to make a simple program.

### 1. Write a triangle Function¶

Write a function to draw a triangle that takes two arguments, tu a turtle, and len the length of a side. The function draws an equallateral triangle with sides of len pixels.

• Name: triangle

• Arguments:

• tu (Turtle) a turtle to draw with.

• len (int) the length of a side

[ ]:




### 2. Draw Using triangle¶

Use the triangle funciton from the last question to construct a figure like this:

Notice that it’s made of three triangles, two at the base and one on top.

[ ]: