Calling functions
Arguments, telling and asking, and notes on the robot's buzzer.
Do this lesson in the simulatorprint("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")
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)
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.
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")
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()
Arguments are separated by commas, and their order matters: led(255, 0, 0) is red, led(0, 0, 255) is blue.
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")
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())
The line battery() on its own did nothing visible. The answer came back and was thrown away. The next line catches it with print.
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 a third of a second: 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
- Flash the LED red, then green, then blue, one second each, then off.
- Print the distance, drive 10 cm, print the distance again.
- 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.