FUNCTION

Top  Previous  Next

Functions are one of the important aspects of VPL programming. By using functions (and function blocks), it is possible to isolate often used pieces of code in small enclosed units. Reuse of previously developed functions enables one to develop programs faster and with far less errors.

 

A function can be declared like this:

 

FUNCTION name : returntype;

 

VAR_INPUT

  variable : type;

   ...

  variable : type;

END_VAR;

 

VAR

  variable : type;

   ...

  variable : type;

END_VAR;

...

END_FUNCTION;

 

 

The return type must be one of the built-in data types of VPL. The VAR_INPUT section declares all the variables that can be assigned a value outside of the function (accomplished by the calling program). These variables cannot be modified from within the function but only read. The VAR section contains private variables of the function. The variables that are declared within the VAR section can only be seen from within the function itself. The variables can be assigned an initial value using the ':=' notation.

 

Below is an example of a simple function that can add 3 numbers together and return the result:

 

FUNCTION Add : INT;

 

VAR_INPUT

    a : INT;

    b : INT;

    c : INT;

END_VAR;

 

    Add := a+b+c; // Make the our function return the sum of the 3 numbers

 

END_FUNCTION;

 

The return value of the function will be the value that is assigned to the name of the function as shown above.

 

The above function can then be used in a program in the following way:

 

j := Add(a:=3, b:=4, c:=10);

 

After this call, j will have the value 17.

 

Please note how the variables of the function is assigned values by stating their names. If you omit one or more of the variables when calling the function, the initial value of the variables (assigned in the functions VAR_INPUT section with the := notation) will be used instead. If no initial value is given for the function, the value of 0 will be used instead (for a STRING an empty string will be used instead).

 

An example:

 

j := Add(c:=5,a:=2);

 

This call will use the initial value for b (in this case 0). Therefore, the return value of the call (which will be assigned to j) will be 7.

 

Please also note that it does not matter where in the list each of the variables is mentioned. This is one of the benefits of using this notation. It is possible to assign the variables in any desired order.

 

The standard parameter passing method is by value. When a parameter is passed by value, a copy of the parameter is made. Therefore, changes made to the formal parameter by the called function have an effect on the corresponding actual parameters.

It is also possible to pass parameters to a function by reference by using the ACCESS parameter attribute. When a parameter is passed by reference, conceptually, the actual parameter itself is passed (and just assigned a new name - the name of the corresponding parameter). Therefore, any changes made to the formal parameter do affect the actual parameter. Please see the information on the ACCESS keyword for additional information.

 

It is possible to separate the interface and implementation part of a function using the INTERFACE / IMPLEMENTATION functionality.