6.2. Function Parameters

Recall the main function from the last section that draws a square:

 
1
import turtle
2
3
def main() -> None:
4
    for i in range(0, 4, 1):
5
        turtle.forward(50.0)
6
        turtle.left(90.0)
7
    return None
8
9
main()
10

(ch04_2_1)

It is not any better than a program that draws a square, without a function. In fact, it is probably worse because it includes lots of code that is not necessary for drawing a square. To make functions more useful we need to create functions that don’t always do the same thing. We need function with input. For example, the draw square function would be more useful if you could specify the size of the square it draws. The following is a new draw square function that has input:

14
 
1
import turtle
2
3
def draw_square(size: float) -> None:
4
    for i in range(0, 4, 1):
5
        turtle.forward(size)
6
        turtle.left(90.0)
7
    return None
8
9
def main() -> None:
10
    draw_square(50.0)
11
    return None
12
13
main()
14

(ch04_2_2)

The inputs to a function, or the parameters are listed inside the parenthses after the function name. Parameter definitions are similar to variable definitions. They are the parameter name and type separated with a colon. In the above draw_square function, there is one parameter, called size, with type float.

The value of a parameter is specified when the function is called. In the above example, the draw_square function is called on line 10 and it specifies a value of 50.0 for the size parameter inside of the parentheses.

Think of parameters like variables, except you do not specify the value of the parameters anywhere inside the function. Instead the parameter values are specified when the function is called.

The figure below shows this relationship. A function needs certain information to do its work. These values, often called arguments are passed to the function by the user.

../_images/blackboxproc.png

This type of diagram is often called a black-box diagram because it only states the requirements from the perspective of the user. The user must know the name of the function and what arguments need to be passed. The details of how the function works are hidden inside the “black-box”.

Suppose we’re working with turtles and a common operation we need is to draw squares. It would make sense if we did not have to duplicate all the steps each time we want to make a square. “Draw a square” can be thought of as an abstraction of a number of smaller steps. We will need to provide one piece of information for the function to do its work: a size for the side of the square. We could represent this using the following black-box diagram.

../_images/turtleproc.png

Once we’ve defined a function, we can call it as often as we like and its statements will be executed each time we call it. In this case, we could use it to get one of our turtles to draw a square and then we can move the turtle and have it draw a different square in a different location. Note that we lift the tail so that when the turtle moves there is no trace. We put the tail back down before drawing the next square. Make sure you can identify both invocations of the draw_square function.

21
 
1
import turtle
2
3
def draw_square(size: float) -> None:
4
    for i in range(0, 4, 1):
5
        turtle.forward(size)
6
        turtle.left(90.0)
7
    return None
8
9
def main() -> None:
10
    draw_square(50.0)          # Call the function to draw the square
11
12
    turtle.penup()
13
    turtle.setposition(100.0, 100.0)
14
    turtle.pendown()
15
16
    draw_square(75.0)           # Draw another square
17
18
    return None
19
20
main()
21

(ch04_2_1a)

There are a few restrictions on the use of functions that are worth knowing:

  1. Variable declarations in a function must be before all other code.

  2. Functions can not contain import statements.

  3. The last line of a function must be return statement (return None for now)

  4. Functions must be defined above the function from which they are called.

The main function is a special function with it’s own set of rules:

  1. Any program that defines a function must define the main function.

  2. The main function is the only function that can be called from outside any function.

  3. The call to the main function must be the last line in the program.

Warning

Even if a function call needs no arguments, the parentheses ( ) after the function name are required. This can lead to a difficult bug: A function name without the parenthesis is a legal expression referring to the function; for example, print and turtle.setposition, but they do not call the associated functions.

Check your understanding

func-1-1: Which of the following is a valid function header (first line of a function definition)?






func-1-2: What is the name of the following function?

def draw_square(size: float):
    for i in range(0, 4, 1):
        turtle.forward(size)
        turtle.left(90.0)





func-1-3: What are the parameters of the following function?

def draw_square(size: float, color: str):
    turtle.fillcolor(color)
    turtle.begin_fill()
    for i in range(0, 4, 1):
        turtle.forward(size)
        turtle.left(angle)
    turtle.end_fill()





func-1-4: Considering the function below, which of the following statements correctly invokes, or calls, this function (i.e., causes it to run)?

def draw_square(size: float, color: str):
    turtle.fillcolor(color)
    turtle.begin_fill()
    for i in range(0, 4, 1):
        turtle.forward(size)
        turtle.left(angle)
    turtle.end_fill()






You have attempted 1 of 8 activities on this page
Next Section - 6.3. Functions that Return Values