The worksheetDownload the PDF
Answers

F4.1 Writing functions

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

BugBotLab

What this lesson is about

def, calling, and why subprograms help.

Questions 6 marks in all

  1. [1 mark]What happens when Python reaches def report(): and its indented body?

    1. AIt defines the function, but nothing in the body runs yet
    2. BIt runs the body once
    3. CIt runs the body forever
    4. DIt prints report
    Answer: A. Defining is like writing a recipe. The body runs when the function is called: report().
  2. [1 mark]Which are good reasons to write a function?

    Tick every answer that is true.

    1. AWrite a piece of behaviour once and reuse it
    2. BA name like is_clear(40) explains what the code does
    3. CSmall pieces are easier to test
    4. DIt makes the robot faster
    Answer: A, B, C. Functions stop repetition, name ideas and split a program into testable pieces. Speed is not the reason.
  3. [1 mark]What does this program print?

    def signal():
        print("  beep")
    
    print("before")
    signal()
    print("between")
    signal()
    Answer:
    before
      beep
    between
      beep

    At each call the program jumps into the function, runs its body, and comes back to the next line.

  4. [1 mark]A program calls signal() on line 3 and defines def signal(): on line 5. What happens?

    1. ANameError: the function is not defined yet when line 3 runs
    2. BIt works, Python looks ahead
    3. CSyntaxError
    4. DNothing is printed and there is no error
    Answer: A. Python runs top to bottom, so a function must be defined before the line that calls it.
  5. [1 mark]What is a subprogram?

    1. AA named block of code that does a task and can be called from elsewhere in the program
    2. BA program that runs on another computer
    3. CA comment above some code
    4. DThe first line of a program
    Answer: A. Procedures and functions are both subprograms.
  6. [1 mark]In OCR's Exam Reference Language, which word ends a procedure? (one word)

    Answer: endprocedure. procedure name() ... endprocedure. A function ends with endfunction.

The task: signal at the corners

Write a function signal() that turns the LED red, plays a short note, and turns the LED off. Then drive a square of 25 cm sides, calling signal() at every corner. Call your function; do not repeat its lines.

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

def signal():
    led("red")

forward(60, distance=25)
turn_right(30, angle=90)

The hint students can ask for: Write the signal once as a function, then call it at each corner from a loop that drives and turns.

A solution

from bugbot import *
connect()
def signal():
    led("red")
    tone(880, 0.2)
    led("off")

for i in range(4):
    forward(60, distance=25)
    signal()
    turn_right(30, angle=90)

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.