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

Fix the Function

The dog's owner wrote a program for another corridor, with the two functions from the Two Functions exercise. Run it: the dog goes around both hedges and reaches the goal, but it comes back out of every niche without the bone.

The same mistake in three places is a sure sign that the bug is in a function. The program doesn't hold the trip into a niche three times. It holds it once, under Define getBone, and each time the function is called, the dog carries out those blocks, mistake included. A bug in a function shows up wherever the function is called.

The good news: that's also why one fix is enough. Find the bug under Define getBone and fix it there, and the dog gets the bone out of every niche. If the program had the blocks of the trip copied three times instead, you would have to fix every copy, and it's easy to forget one.

To find the bug, go through the blocks of the function in your head as the dog would, starting where it stands just before a call: in the corridor, next to a niche. Where should it pick up the bone?

Instructions

  1. 1

    Fix the function so that the dog picks up all three bones.

  2. 2

    Check that the dog still walks to the goal.

Back

7/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.