Consider the following example script involving a global variable,
age
, and the below-createdhave 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:
- Now when you run the first script, it will provide the output we wanted.