Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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:

  1. Turn right ↻
  2. Move forward
  3. Pick up bone
  4. Turn right ↻
  5. Turn right ↻
  6. Move forward
  7. Turn 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. 1

    Define getBone and use it to pick up all three bones.

  2. 2

    Walk to the goal.

  3. 3

    Use at most 29 blocks.

Back

5/9

  1. 1. Teach the Dog a Trick
  2. 2. A Function in a Loop
  3. 3. A Loop in a Function
  4. 4. Quiz: Calling a Function
  5. 5. Two Functions
  6. 6. A Function That Searches
  7. 7. Fix the Function
  8. 8. Quiz: A Bug in a Function
  9. 9. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.