Previous Up Next

12.1.2  Functions: function endfunction { } local return

You have already seen functions defined with :=. For example, to define a function sumprod which takes two inputs and returns a list with the sum and the product of the inputs, you can enter:
Input:

sumprod(a,b):= [a+b,a*b]

Afterwards, you can use this new function. Input:

sumprod(3,5)

Output:


8,15

You can define functions that are computed with a sequence of instructions by putting the instructions between braces, where each command ends with a semicolon. To use local variables, you can declare them with the local keyword, followed by the variable names. The value returned by the function will be indicated with the return keyword. For example, the above function sumprod could also be defined by:

sumprod(a,b):= {
local s, p;
s:= a + b;
p:= a*b;
return [s,p];
}

Another way to use a sequence of instructions to define a function is with the functionendfunction construction. With this approach, the function name and parameters follow the function keyword. This is otherwise like the previous approach. The sumprod function could be defined by:
Input:

function sumprod(a,b)
local s, p;
s:= a + b;
p:= a*b;
return [s,p];
endfunction

Previous Up Next