Misconception: Students incorrectly believe that variables passed into a block in Snap! will get modified; however, a copy of the passed-in variable is what the block receives and modifies.

  • Consider the following example script involving a global variable, age, and the below-created have a birthday() block:

    Code that uses the have a birthday() blockThe full have a birthday() block
  • When the above script runs, the variable age doesn’t change!

    • Uh, oh! Why is this?

  • In Snap!, when you make a new block, that block has its own copy of the variable.

    • In the example above, the block have a birthday is passed the value 17, not “the variable” age.

    • This is actually really useful! It allows us to write programs that don’t accidentally overwrite values we want to remain unchanged.

  • The solution to make the variable passed in change is to add block a “reporter” block, which outputs a new value. Like this:

    To modify the input variable, we add a reporter block to the have a birthday() block
  • Now when you run the first script, it will provide the output we wanted.