Propeller Manual

Table Of Contents
2: Spin Language Reference – RETURN
Propeller Manual v1.1 · Page 197
Using RETURN
The following example demonstrates two uses of
RETURN. Assume that
DisplayDivByZeroError is a method defined elsewhere.
PUB Add(Num1, Num2)
Result := Num1 + Num2 'Add Num1 + Num2
return
PUB Divide(Dividend, Divisor)
if Divisor == 0 'Check if Divisor = 0
DisplayDivByZeroError 'If so, display error
return 0 'and return with 0
return Dividend / Divisor 'Otherwise return quotient
The Add method sets its built-in RESULT variable equal to Num1 plus Num2, then executes
RETURN. The RETURN causes Add to return the value of RESULT to the caller. Note that this
RETURN was not really required because the Propeller Tool Compiler will automatically put it
in at the end of any methods that don’t have one.
The
Divide method checks the Divisor value. If Divisor equals zero, it calls a
DisplayDivByZeroError method and then executes return 0, which immediately causes the
method to return with the value 0. If, however, the
Divisor was not equal to zero, it executes
return Dividend / Divisor, which causes the method to return with the result of the
division. This is an example where the last
RETURN was used to perform the calculation and
return the result all in one step rather than separately affecting the built-in
RESULT variable
beforehand.