RESULT

Variable: The return value variable for methods.

((PUBPRI))
   RESULT

Explanation

The RESULT variable is a pre-defined local variable for each PUB and PRI method. RESULT holds the method's return value; the value passed back to the caller of the method, when the method is terminated.
When a public or private method is called, its built-in RESULT variable is initialized to zero (0). If that method does not alter RESULT, or does not call RETURN or ABORT with a value specified, then zero will be the return value upon that method's termination.

Using RESULT

In the example below, the DoSomething method sets RESULT equal to 100 at its end. The Main method calls DoSomething and sets its local variable, Temp, equal to the result; so that when DoSomething exits, Temp will be set to 100.

PUB Main | Temp
  Temp := DoSomething            'Call DoSomething, set Temp to return value
 
PUB DoSomething
  <do something here>
  result := 100                  'Set result to 100

You can also provide an alias name for a method's RESULT variable in order to make it more clear what the method returns. This is highly recommended since it makes a method's intent more easily discerned. For example:

PUB GetChar : Char
  <do something>
  Char := <retrieved character>  'Set Char (result) to the character

The above method, GetChar, declares Char as an alias for its built-in RESULT variable; see PUB or PRI for more information. The GetChar method then performs some task to get a character then it sets Char to the value of the retrieved character. It could have also used "result := …" to set the return value since either statement affects the method's return value.

Either the RESULT variable, or the alias provided for it, may be modified multiple times within the method before exiting since they both affect RESULT and only the last value of RESULT will be used upon exiting.

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.