< Back

Activity 8

Return

Fruitful function are functions that have output, or return a value, as opposed to just performing some action. Up to this point, you have only been using the latter. However, fruitful functions allow great code reusability. In order to use them, you need to know what happens with that return value. Let's explore return statements a bit more closely.

Details

  1. Fill in the following table. For each row of the table, Write what the following function prints to the terminal window. Put the result in the column on the right.

    ExecuteResult
    print(turtle.goto(0, 0))
    print(turtle.pos())
    print(math.sin(0))
    print(turtle.forward(100))
    print(max(3, 4))
  2. Which of the above functions do you think are fruitful functions?

  3. Consider the following Python programs. Write what you think each program will print to the terminal.

    ProgramOutput
    def compute_sum(x, y):
        x + y
    
    compute_sum(1, 1)
    
    def compute_sum(x, y):
        x + y
    
    print(compute_sum(1, 1))
    
    def compute_sum(x, y):
        return x + y
    
    compute_sum(1, 1)
    
    def compute_sum(x, y):
        return x + y
    
    print(compute_sum(1, 1))
    
    def compute_sum(x, y):
        return x + y
    
    my_sum = compute_sum(1, 1)
    print(my_sum)
    
    def compute_sum(x, y):
        return x + y
        return x + x + y
        return x + x + y + y
    
    my_sum = compute_sum(1, 1)
    print(my_sum)