Two Functions
Here's another corridor with hedges in the way. This one also has niches like those in the Side Trips exercise, this time in the hedge on the dog's right, with a bone in each. The editor already holds the function Define goAround from Teach the Dog a Trick. For the bones, the dog needs a second trick, a trip into the niche and back:
Turn right ↻Move forwardPick up boneTurn right ↻Turn right ↻Move forwardTurn right ↻
The two turns in the middle turn the dog around, and the last one points it along the corridor again.
A program can have as many functions as it needs. Drag another Define block into the editor. It comes with a name that no function has yet, so that the two can't be mixed up. Rename it getBone. The column of blocks then offers a call of each function: goAround and getBone.
With good names, the program under Start reads almost like a description of the way: forward, get a bone, forward, go around… You can tell what the dog does without going through every turn in your head. That's another reason programmers use functions: a long program split into named parts is easier to read and to check.
By the way, a Define block can hold any blocks, calls of other functions too. A function can use another function, just as your program uses them.
Instructions
- 1
Define
getBoneand use it to pick up all three bones. - 2
Walk to the goal.
- 3
Use at most 29 blocks.