Fundamentals of NewtonScript

Methods


func(parameterFirst, ..., parameterLast)
begin
code
end


A method is simply a slot within a frame that contains a function. Here is an example of a method, Max, that returns the larger of two values:

aFrame := {
   Max: func(a, b)
   begin
      if b > a then 
         return b
      else 
         return a;
   end,
};
A function always returns a value. If an executing function has a return statement, then that is the function's value. If the executing function does not have a formal return statement, then the last executing statement is the function's value. A couple of examples should make this clear. You could rewrite the above Max function in this way:

func(a, b)
begin
   if b > a then 
      b
   else 
      a;
end;
The return statements can be removed because the value of the if statement is either the value of b or the value of a (see "if/then/else Statements" on page 71). You can produce an even more minimal Max:

func(a, b)
   if b > a then 
      b
   else 
      a;
With one-statement functions, the begin and end are unnecessary. Since the if/else is now only one statement, you can snip the begin and end.

While this last form of Max is the smallest, it is not necessarily the best. Most people find the original Max easier to read.

Local Variables
Message Sending

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

Last modified: 1 DEC 1996