Calling functions

Arguments, telling and asking, and notes on the robot's buzzer.

F1.3Programming basicsGCSE15 min

Covers OCR J277 2.2.3 Additional programming techniques · AQA 8525 3.2.10 Structured programming and subroutines (procedures and functions) · Edexcel 1CP2 6.6.1 Built-in, library and user-devised subprograms

Do this lesson in the simulator

print("hi"), forward(50), distance(): every instruction you have used so far has the same shape, a name followed by brackets. That shape is a function call, and it is how you make anything happen in Python.

A name, then brackets

The name says what to do. The brackets say do it now. Whatever is inside the brackets is extra information for the job:

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

# the LED: red
led("red")
# pause 1 s (the robot keeps doing what it was told)
wait(1)
# the LED: green
led("green")
# pause 1 s
wait(1)
# the LED: off
led("off")

Run this in the simulator

led needs to know which colour: that is the information inside the brackets. wait needs to know how long. Each thing inside the brackets is called an argument.

Arguments you can hear

The simulated BugBot has a piezo buzzer, a small disc that clicks back and forth to make a note (the real robot gets one in a later board). tone takes two arguments: how high the note is, in hertz, and how long it lasts, in seconds. Turn your sound on and run this:

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

# the note A for half a second
tone(440, 0.5)
# a higher A, twice the frequency: one octave up
tone(880, 0.5)
# a lower one
tone(220, 0.5)

Run this in the simulator

Change the numbers and listen. The first argument changes the note, the second changes how long it plays. If you cannot play sound, watch the note appear under the robot as it plays.

The call tone(440, 0.5): the name, the brackets, and two arguments separated by a commatone(440,0.5)the name: what to dothe brackets: do it nowargument 1:how high, in hertzargument 2:how long, in secondsa comma between them
The parts of one call. The name says what to do, the brackets say do it now, and each argument is one piece of information for the job, in its own place.

No brackets, nothing happens

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

# the LED: blue
led("blue")
print
# the LED: off
led("off")

Run this in the simulator

The bare print did nothing, and Python did not complain either. Without brackets you are only naming the function, not calling it. This is one of the quiet mistakes: no error, just nothing.

Some functions need nothing, some need several things

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

# red, as three numbers: red, green, blue, each 0 to 255
led(255, 0, 0)
wait(1)
# orange
led(255, 128, 0)
wait(1)
# all motors off: no information needed, but the brackets still have to be there
stop()

Run this in the simulator

Arguments are separated by commas, and their order matters: led(255, 0, 0) is red, led(0, 0, 255) is blue.

Three led calls: each number goes to red, green or blue by its place in the bracketsredgreenblueled(255, 0, 0)25500redled(0, 0, 255)00255blueled(255, 128, 0)2551280orange
The first number always goes to red, the second to green and the third to blue, from 0 (off) to 255 (full). Swap two numbers and a different light goes on.

Some arguments have names

forward can be given a distance as a named argument:

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

# drive forward at 50 for 20 cm, then stop
forward(50, distance=20)
print("that was 20 cm")

Run this in the simulator

The name tells Python which extra information you mean. You will see this shape a lot: name=value inside the brackets.

Telling versus asking

led("red") tells the robot to do something. battery() asks the robot something, and the robot answers with a value: we say the function returns a value. An asking function is only useful if you do something with the answer, such as print it:

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

# the robot answers, but nobody is listening
battery()
# now the answer is printed
print(battery())
print("ahead:", distance())

Run this in the simulator

The line battery() on its own did nothing visible. The answer came back and was thrown away. The next line catches it with print.

led takes an argument in and sends nothing back; battery takes nothing in and sends a value back outtelling"red"argument inledthe lightgoes rednothing comes backasking( ) nothing inbatterya value comes backthe percent leftprint shows itnot used: lost
A telling function takes information in and acts. An asking function hands a value back out. That value is lost unless something, such as print, uses it.

The robot's functions so far

Function Tells or asks What it does
print(...) tells shows text or numbers in the console
led(colour) or led(r, g, b) tells sets the light
tone(hertz, seconds) tells plays a note on the piezo
forward(speed) and forward(speed, distance=cm) tells drives
stop() tells motors off
wait(seconds) tells pauses the program
battery() asks percent left
distance() asks centimetres to the nearest thing ahead

Task: lights and numbers

Make the LED blue, then print two lines: battery: <number> and ahead: <number>, using the robot's answers. The robot must not drive.

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

led("...")
print("battery:", ...)

Task: three notes

Play the notes C, E and G, one after another, each for 0.3 seconds: 262, 330 and 392 hertz. The robot must not drive.

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

tone(262, 0.3)

Challenges

  1. Flash the LED red, then green, then blue, one second each, then off.
  2. Print the distance, drive 10 cm, print the distance again.
  3. Play the first line of a tune you know. Middle C is 262 Hz, D 294, E 330, F 349, G 392, A 440, B 494.

Past paper questions on this

Real exam questions that test this, each worked through with a program you can run.