functions

Describe the use of functions within extenso

FUNCTIONS

NAME

functions - Describes the use of functions within Extenso.

SYNOPSIS

function id () … endf

function f(p1, p2, … ; n1, n2, …) …endf

return expression;

DESCRIPTION

A function is used to define a segment of code that will be re-used with different arguments. A function always returns something. If no returned expression is specified, than a null variable is returned.

Variables within functions are local and go out of scope when the function returns.

Parameters are passed by value.

User’s function are defined using the following syntax:

function id ()
        ...
endf

or

function f(p1, p2, ... ; n1, n2, ...)
    ...
endf

In the first form there is no parameter.

In the second form, the first part are the positionnal parameters and the second part are the name parameters.

The definition of the name parameters assure that all parameters specified are defined. If … is specified, then you can have more named parameters. If nothing is specified, no named parameters are authorised.

The following variables are defined in a function:

  • sn_argcp : number of positionnal parameters
  • sn_argcn : number of named parameters
  • argc : Number of argument
  • sn_argsn : Associative array of named parameters
  • sn_argsp : Array of positionnal parameters

if … is used as an argument, then you can call the function with parameters not defined.

Under the form f(…;…) no verification is none.

Here is an example of a function call.

id(arg1, arg2, arg3:val3, ...);

Parameters can be named or positionnals.

To access parameters within your function you can use:

sn_argsp[0]..sn_argsp[sn_argcp]

The variable sn_argcp contains the number of positionnal parameters.

Named parameters can be used as follows:

sn_argsn.n1
sn_argsn.n2
...

The variable sn_argcn contains the number of named parameters.

"Return" in a function is used to return a value.

If sn_loop is set as true, then this function is used in a callback.

In that case, the following variables are also defined within the function:

  • sn_nb : loop counter starting from 0.
  • sn_nb1 : loop counter starting from 1.
  • sn_varname : Variable name used in the loop.
  • sn_var : Variable for the loop.

The variable sn_var can be used to access the callback variable.

A function can also be called with a variable by using the character @ in front of the function name as in:

@f(); or @f(1,2,3,4);

If the value of variable f contains a valid function name, this function will be called.

EXAMPLES

Note: In the followings examples, the _ between the { should be removed to make it work.

function two_params(one,two)
    param1 = one;
    param2 = two;
    res=[param1,param2];
    
    return res;
endf

USE of this function:
res = two_params(1,2);
res[0]; will give 1
res[1]; will give 2

-----------------------------

res={_{
function g(j)
    ' g='; sn_nb; sn_loop;
        return j*2;
endf

function f(i)
        ' f i='; i;
        ' sn_nb='; sn_nb; sn_loop;
        sn_var={ 'x' : g(i) };
        return i>0;
endf

k=2;
for j f(k)  do
        k--;
    " j="; j; j.type();
endfor

}}.

return
res= f i=2 sn_nb=0true g=false j={"x":4}context f i=1 sn_nb=1true g=false j={"x":2}context f i=0 sn_nb=2true g=false.

q(res={_{
        function test(p1,p2;n1,n2)
                "p1="; p1;
                "p2="; p2;
                "n1="; n1;
                "n2="; n2;
        endf

        test("a","b","n1":"vn1","n2":"vn2");
        }}. return  res=p1=ap2=bn1=vn1n2=vn2.

SEE ALSO

AUTHOR

Written by Pierre Laplante and Caroline Laplante, <laplante@sednove.com>

MODIFICATIONS

1.0 2014-09-09 21:24:14 laplante@sednove.com

1.1 2015-10-15 laplante@sednove.com Add indirect function call

Edit

© 2024 extenso Inc. All rights reserved.