Next Previous Contents

3. Other statements

3.1 call

Reserved word: call

Function call syntax:

    [y1,y2..] = f(x1,x2..)
calls f with input arguments x1,x2.. and outputs arguments y1,y2.. .

If f(x1,x2..) appears alone in an expression, it is effectively called with one output argument, which becomes the value of the expression. f(x1,x2..) is equivalent to

    call(f,x2,x2..).
This notation makes it possible to call functions indirectly through variables, and to write functionals.

See also: function

3.2 function

Reserved word: function Also uses special tokens: [ ] ( ) , ... ;

Function definition syntax:

    function [y1,y2..] = f(x1,x2..) LOCAL-DECL { stmt-sequence } ;
or
    function y = f(x1,x2..) LOCAL-DECL { stmt-sequence } ;
or
    function f(x1,x2..) LOCAL-DECL { stmt-sequence } ;
where yi are formal output arguments and xi are formal input arguments. See local, global for what LOCAL-DECL may be.

By default, output arguments are optional and input arguments are obligatory. This can be changed by using a semicolon in the argument list. Identifiers before a semicolon become obligatory and identifiers after the semicolon are declared optional.

Example:

    function [y,z;] = f(x) local(a) { /* ... */ };
declares x, y and z obligatory and 'a' a local variable.

An output argument should be declared obligatory if it is also used as input in the function.

The ellipsis sign (...) may be appended to the input or output formal argument list. It is thus possible to define functions with variable number of arguments. See argin, argout, SetArgOut, Nargin, Nargout for details.

You can define as many functions in one input file as you wish. In order to call a Tela-function you must first source the input file containing that function.

See also: local, return, argin, Nargin, special, package.

3.3 local, global

Reserved words: local, global

Variables appearing inside functions, which are not input or output arguments, are either local or global. If they are local, they are similar to function arguments, except that they are initialized with undefined value before entering the function.

The function definition is of the form

    function [out1,out2...] = f(in1,in2...) LOCAL-DECL { /* body */ };
where LOCAL-DECL has one of the following five forms:

1. LOCAL-DECL can be empty, in which case all variables are implicitly local, except autoglobal variables such as pi and eps.

2. LOCAL-DECL can be the keyword 'local'. This is exactly similar to case 1 above.

3. LOCAL-DECL can be the keyword 'global'. This makes all free variables in the function body global.

4. LOCAL-DECL can be of the form 'local(a,b,...)'. This makes variables a,b,... local, and all other free variables global. If an autoglobal variables such as pi or eps is listed, as in 'local(pi)', it overrides the autoglobal attribute, making 'pi' local and uninitialized in the function body. Such practice is not recommended however.

5. LOCAL-DECL can be of the form 'global(a,b,...)'. This makes variables a,b,... global, and all other free variables implicitly local. Autoglobal variables remain global.

Examples:

function f() /*local*/ {y=2; disp y+pi};

Variable y is local, but pi is not since it is autoglobal. When called, f() will output 5.14159, and global y has not been affected. Nothing changes if you uncomment the keyword local.

    function V = ddx(u)
    global(imax,dx)
    {
        V = zeros(imax);
        coeff = 1/(2*dx);
        V[2:imax-1] = (u[3:imax] - u[1:imax-2])*coeff;
        V[1] = V[imax-1];
        V[imax] = V[2];
    };
This example computes the numerical derivative. The variables coeff is local since it is not mentioned in the global list.

See also: function, package.

3.4 package

Reserved word: package

The package mechanism is for hiding names local to a group of functions and other statements from global access. Syntax:

package "mypackage" LOCAL-DECL { /* body */ };

Naming is optional: if the package is not named explicitly, the current input file name is used. If you use one package per one input file, you usually don't have to use any name. LOCAL-DECL is analogous to function definition, see local. Usually LOCAL-DECL will be of the form

package global(fn1,fn2,...) { /* body */ };

where fn1,fn2,... are the globally visible objects of the package. They are typically functions, but they can also be variable names. In the example above, all symbols in body which are not among fn1,fn2,... are hidden from global reference. This is accomplished by replacing the symbols with new symbols. The new symbol names are obtained from the old ones by prepending a prefix "i:", where i is a positive integer unique to each package name. Since a symbol name cannot contain a colon, it is impossible to refer to these symbols from outside. The hidden symbols are also excluded from name completion and whos() function, for instance.

See also: function, local.

3.5 disp

Reserved word: disp

disp expr;
displays expression on standard output, in the same way as typing its name on command line.

Disp is primarily a debugging aid. There are better ways to create output, for example the standard function 'format'.


Next Previous Contents