Next: 3.2.2 Loading Functions
Up: 3.2 Defining and Using
Previous: 3.2 Defining and Using
Function structure must obey the following format
function [y1,...,yn]=foo(x1,...,xm)
.
.
.
where foo is the function name, the xi are the m input arguments
of the function, the yj are the n output arguments from the function, and
the three vertical dots represent the list of instructions performed by
the function. An example of a function
which calculates k! is as follows
function [x]=fact(k)
k=int(k);
if k<1 then,
k=1;
end,
x=1;
for j=1:k,
x=x*j;
end,
If this function is contained in a file called fact.sci the function
must be ``loaded'' into Scilab by the getf command and before
it can be used:
--> exists('fact')
ans =
0.
--> getf('../macros/fact.sci')
--> exists('fact')
ans =
1.
--> x=fact(5)
x =
120.
In the above Scilab session, the command
exists
indicates that
fact is not in the environment (by the 0 answer to exist). The
function is loaded into the environment using getf and now exists
indicates that the function is there (the 1 answer). The example
calculates 5!.
Next: 3.2.2 Loading Functions
Up: 3.2 Defining and Using
Previous: 3.2 Defining and Using
Scilab Group