Text EN Version 2.00

FUNCTIONS

Description

As of version 2.00:

Functions:

A function is defined as GLOBAL or LOCAL.
A GLOBAL function is not deleted from memory after the program that defined the GLOBAL function has ended, and is therefore also available to subsequent programs for calls.
GLOBAL functions do not have access to variables/fields belonging to a document or group that was not opened or created within this GLOBAL function.
(This is not possible even if the GLOBAL function is passed the NOTEHANDLE or the VARGROUP.)
An exception is the VARGROUP GLOBAL; variables of this group are addressable.
If a variable of the VARGROUP GLOBAL is accessed GLOBAL function directly, this variable must not be physically deleted with DELVAR, or in a similar way, between compiling the GLOBAL function and calling it.
(A safe and permitted method is to use the @Functions @GetVar and @SetVar to access such variables. Deleted variables, of course, result in ERROR values or PV contents when addressed with @GetVar/@SetVar.)

Possible consequences of directly addressing GLOBAL variables in GLOBAL DEFFN functions are:
   Server crash.
   Overwriting other variables.
   During program execution, the content of another variable is returned instead of the addressed variable.

This also applies to variables addressed directly via VSTACKPATH.
The names of the currently defined GLOBAL functions can be determined with the @Function @GetDEFFNGlobal.
LOCAL functions can access all variables that are used in the program or in other LOCAL functions and that are assigned to a group or a document.
LOCAL variables of the program or of functions are generally known only to the respective function or the program.
A function can only be defined if a function with the same name does not already exist.
A function name must be unique.
However, it is possible to define a GLOBAL function and a LOCAL function with the same name.

Parameter passing:

A parameter is passed to a function in the same way as to an @Function.
The order of the parameters in the call and in the function definition must be observed.
The names do not have to be identical.
Pure input parameters may also be expressions.
A return parameter must be a real variable (that is, no constant and no expression are allowed).
A return parameter is prefixed with an ampersand & in the call and with an asterisk * in the definition.

Deleting a function:
GLOBAL functions can be deleted again with DELFN.
If a GLOBAL function is called after deletion by a CALL or CALLFAST that was compiled before the deletion, this causes a crash.
If this possibility exists, a CALLSAVE should be used instead.

Calling functions:
For recursive functions, either a CALL or a CALLSAVE should be used, because a CALLFAST does not buffer the LOCAL variables of the function.
A CALL (of any type) to a LOCAL function must always already be linkable at compile time; that is, a LOCAL function must be defined in the program that calls this LOCAL function.
A CALL (of any type) to a GLOBAL function does not have to be linkable at compile time; that is, a GLOBAL function may also be defined later by another program or may already have been defined earlier.
Once a CALL or CALLFAST has been linked, however, it is final; subsequently deleting the function to be called then causes a crash when it is called. If this case is possible, a CALLSAVE must be used.

GLOBAL syntax:

DEFFN:Name[GLOBAL](P1;P2;*P3)
{
}

A GLOBAL function Name is defined with the input parameters P1 and P2 and the return parameter P3.
The parameters P1, P2, and P3 can be treated like normal LOCAL parameters.

DELFN:Name[GLOBAL]();

The GLOBAL function Name is physically deleted from memory.

CALL:Name[GLOBAL](P1;P2;&P3);
CALLFAST:Name[GLOBAL](P1;P2;&P3);
CALLSAVE:Name[GLOBAL](P1;P2;&P3);

The GLOBAL function Name is called.
For P1 and P2, variable names, constants, or expressions other than those in the function definition can also be used (the same names were chosen here only to illustrate the relationship between the parameter positions).
For P3, a variable name other than the one in the function definition can also be used (the same name was chosen here only to illustrate the relationship between the parameter positions).

LOCAL syntax:

DEFFN:Name(P1;P2;*P3)
{
}

A LOCAL function Name is defined with the input parameters P1 and P2 and the return parameter P3.
The parameters P1, P2, and P3 can be treated like normal LOCAL parameters.

CALL:Name(P1;P2;&P3);
CALLFAST:Name(P1;P2;&P3);
CALLSAVE:Name(P1;P2;&P3);

The LOCAL function Name is called.
For P1 and P2, variable names, constants, or expressions other than those in the function definition can also be used (the same names were chosen here only to illustrate the relationship between the parameter positions).
For P3, a variable name other than the one in the function definition can also be used (the same name was chosen here only to illustrate the relationship between the parameter positions).

Example program (PRC) for GLOBAL functions:

@LogReport("Currently defined [GLOBAL] functions:");
@LogReport(@GetDEFFNGlobal);
Test[VARGRP1]:="Vargroup Test";
CALL:Test1[GLOBAL]("Hello input parameter";&Back);
@LogReport(Back);
CALLFAST:Test1[GLOBAL](100+10*3;&Ret);
@LogReport(Ret);
Tralala:="Tralala";
CALLSAVE:Test1[GLOBAL](Tralala;Hupfdidupf);
@LogReport(Hupfdidupf);

CALL:Test2[GLOBAL]("Text1";1234;&R1;&R2);
@LogReport(R1);
@LogReport(R2);
CALLFAST:Test2[GLOBAL]("Text"+"2 ";@Sqrt(100);&R1;&R2);
@LogReport(R1);
@LogReport(R2);
I1:="Input Para 1";
I2:=6666;
CALLSAVE:Test2[GLOBAL](I1;I2;&R1;&R2);
@LogReport(R1);
@LogReport(R2);

DELFN:Test1[GLOBAL]();
DEFFN:Test1[GLOBAL](Input;*Return)
{
   @LogReport("Test1[GLOBAL] function start");
   @LogReport(Test[VARGRP1]);
   @LogReport(Input);
   Return:="Return Text";
   @LogReport("Test1[GLOBAL] function end");
}

DELFN:Test2[GLOBAL]();
DEFFN:Test2[GLOBAL](Text;Zahl;*Ret1;*Ret2)
{
   @LogReport("Test2[GLOBAL] function start");
   @LogReport(Test[VARGRP1]);
   @LogReport(Text);
   @LogReport(Zahl);
   Ret1:=Text+@Text(Zahl);
   Ret2:=Zahl*2;
   @LogReport("Test2[GLOBAL] function end");
}

Example program (PRC) for LOCAL functions:

Test[VARGRP1]:="Vargroup Test";
CALL:Test1("Hello input parameter";&Back);
@LogReport(Back);
CALLFAST:Test1(100+10*3;&Ret);
@LogReport(Ret);
Tralala:="Tralala";
CALLSAVE:Test1(Tralala;Hupfdidupf);
@LogReport(Hupfdidupf);

CALL:Test2("Text1";1234;&R1;&R2);
@LogReport(R1);
@LogReport(R2);
CALLFAST:Test2("Text"+"2 ";@Sqrt(100);&R1;&R2);
@LogReport(R1);
@LogReport(R2);
I1:="Input Para 1";
I2:=6666;
CALLSAVE:Test2(I1;I2;&R1;&R2);
@LogReport(R1);
@LogReport(R2);

DEFFN:Test1(Input;*Return)
{
   @LogReport("Test1 function start");
   @LogReport(Test[VARGRP1]);
   @LogReport(Input);
   Return:="Return Text";
   @LogReport("Test1 function end");
}

DEFFN:Test2(Text;Zahl;*Ret1;*Ret2)
{
   @LogReport("Test2 function start");
   @LogReport(Test[VARGRP1]);
   @LogReport(Text);
   @LogReport(Zahl);
   Ret1:=Text+@Text(Zahl);
   Ret2:=Zahl*2;
   @LogReport("Test2 function end");
}

Note : This text was machine-translated and may contain inaccuracies.