Consider the following example script involving a global variable,
age, and the below-created
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 birthdayis passed the value 17, not “the variable”
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:
- Now when you run the first script, it will provide the output we wanted.
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.