MatchTopParameters vs. Instance VariablesLocal Variables and definitions

Local Variables and definitions

As we have already seen in a few examples, we can also put def and var declarations inside methods (we cannot put method definitions inside other method definitions - unless they are part of some bigger object being declared inside the method).

A good example of a variable that should have been declared local is the variables used to generate a new cloth color in part 1 of the Laundry lab. Rather than look at that program, we will look at similar code that appears if we try to modify our SpiroGraph program for use on July 4th (i.e. we make it draw in the three colors red, white and blue)

Local variables and refs can only be seen and used within the body of the method they are declared in, and go out of existence when the method terminates. Thus they are great to hold temporary values that will be needed later in the same method, but, like parameters, go away after the method terminates.


MatchTopParameters vs. Instance VariablesLocal Variables and definitions