Methods

Local Variables

Any of your functions can have local variables using the local syntax:

func(a, b, c)
begin
   local maxSoFar := a;

   if b > maxSoFar then maxSoFar := b;
   if c > maxSoFar then maxSoFar := c;
   return maxSoFar;
end;
It is not necessary to assign a value to a local variable at the time you declare it. All locals are initialized to nil at the beginning of a function.

The scope of a local variable is the entire function. Though this may seem weird, you can use variables before they are declared:

func(a, b, c)
begin
   maxSoFar := a;

   if b > maxSoFar then maxSoFar := b;
   if c > maxSoFar then maxSoFar := c;
   return maxSoFar;
   local maxSoFar;
end;
However, good style dictates that you declare your local variables before you use them; otherwise, readers of your code will be quite confused.


An online version of Programming for the Newton using Macintosh, 2nd ed. ©1996, 1994, Julie McKeehan and Neil Rhodes.

Last modified: 1 DEC 1996