© Tyl Software Foundation 2019-2021
▶ FUNCTION RETURN STATEMENT
Function return statements are used when the execution of the enclosing function
needs to be stopped immediately, and a result has to be returned to the
function caller.
To make a function return statement, write:
To make a function return statement, write:
%% num
Tyl symbolizer will replace the double percent code '%%', with
'✖✖', the function break symbol:
✖✖ num
Consider this program:
go:
maxnumresult maxnum 2 3
print maxnumresult
maxnum num1 num2:
num2 > num1 ? ✖✖ num2
num1
3
maxnum
function gets two numbers and returns the greater of them. If num2
is greater than num1
, the function return statement '%% num2
', will
be executed, maxnum
function running will stop, and num2
will be returned,
otherwise, the code will continue to line num1
, and that will be the returned
value of the function.
Generally, function return statement is used to return values before function end.
This is a key value mapping (hashmap) implementation:
go:
citycode 4524
city citymap citycode
city ? print 'selected city: (' + citycode + ') ' + city
citymap key:
key 1491 ? ✖✖ 'Tokyo'
key 2502 ? ✖✖ 'Timboktu'
key 3513 ? ✖✖ 'Tallahassee'
key 4524 ? ✖✖ 'Thule'
selected city: (4524) Thule
citymap
is a mapping function, that returns a value for key. It is
composed of conditional statements with function return statement as
their yes block.
The return statement can be any statement. Consider this:
go:
num 20
i 4 ~ print addbystep ( num -- 4 ) 10
addbystep num step:
num > step ? %% addtwo num
addone num
addone num: num + 1
addtwo num: num + 2
18
14
9
5
14
9
5
addbystep
function return statement is an operation on num
, based on
step
parameter. In the looping statement 'i 4 ~
', num
variable of go
function is decremented by 4, using the double minus operator '--
',
and is fed to addbystep
along with a fixed step 10.
As long as num
is greater then step
, addbystep
returns the result of
addtwo num
, otherwise the result of addone num
.