The answersDownload the PDF
Worksheet

F4.2 Parameters and return values

Functions and structured code · GCSE · OCR J277 2.2.3, AQA 8525 3.2.10, Edexcel 1CP2 6.6.2 · about 15 min

BugBotLab
NameClassDate

What this lesson is about

Information in and an answer out; procedures and functions.

Questions 7 marks in all

  1. [1 mark]What does this program print?

    def double(n):
        return n * 2
    
    print(double(4))
    print(double(10))
  2. [1 mark]In def square(size):, what is size?

    1. AA parameter: it gets its value from the call
    2. BAn argument
    3. CA return value
    4. DA comment
  3. [1 mark]What does this program print?

    def greet(name, times):
        for i in range(times):
            print("hi", name)
    
    greet("Ada", 2)
  4. [1 mark]What does this program print?

    def double_print(n):
        print(n * 2)
    
    def double_return(n):
        return n * 2
    
    a = double_print(5)
    b = double_return(5)
    print(a, b)
  5. [1 mark]In area(20, 30), what are 20 and 30?

    1. AArguments
    2. BParameters
    3. CReturn values
    4. DLocal variables
  6. [1 mark]What is the difference between a function and a procedure?

    1. AA function returns a value; a procedure does not
    2. BA procedure has parameters; a function cannot
    3. CA function is written in capitals
    4. DThere is none in any language
  7. [1 mark]What does this program print?

    def check(n):
        if n > 10:
            return "big"
        return "small"
    
    print(check(15), check(3))

The task: any polygon

Write polygon(sides, length) that prints polygon with <sides> sides and drives the shape. Call it for a pentagon (5 sides, 20 cm) and then a triangle (3 sides, 20 cm). Finish facing roughly the way you started.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def polygon(sides, length):
    print("polygon with", sides, "sides")
    # drive it

polygon(5, 20)

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/f4-2-parameters-and-return-values/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Write average(a, b, c) that returns the average of three numbers, and test it with values you can check.
  2. Give polygon a third parameter for the speed.
  3. Write creep(limit) that drives in 5 cm steps until the wall is under limit cm away, and returns how many steps it took.