ACCESS

Top  Previous  Next

 

The ACCESS keyword is used in combination with input parameters to a function to support parameter passing by reference. The standard parameter passing method to a function is by value.

 

This is a simple example of using reference parameter passing:

 

FUNCTION TEST;

VAR_INPUT

  v1 : ACCESS INT;

  v2 : ACCESS STRING;

  v3 : INT;

END_VAR;

  v1 := v1 * 5;

  v2 := "Hello world";

        v3 := 51;   <--- NOT ALLOWED

END_FUNCTION;

 

VAR

  x1 : INT := 1;

  x2 : INT := 1;

  s : STRING := "Nice";

END_VAR;

 

Calling the function:

 

TEST( v1 := x1, v2 := s,  v3 := x2 );

 

 

After the call to "test", the contents of the parameters passed to the function are as follows: "x1" is 5, "x2" is 1, and "s" is "Hello world".

Please note that "x2" did not change its value as it was passed by value. "x1" and "s" were both passed by reference and therefore the changes for the actual parameters to "TEST" changed accordingly.

Actually it is not possible for "TEST" to update the "v3" parameter as changes to VAR_INPUT by value parameters in a function are not allowed. Using the ACCESS attribute on a VAR_INPUT variable will, however, change this rule and allow updating of the referenced parameter.

 

By using the ACCESS attribute, it is possible to pass a STRUCT_BLOCK to a function:

 

 

STRUCT_BLOCK server_pack;

  mode         : SINT;

  linsec       : DINT;

  latitude     : DINT;

  longitude   : DINT;

END_STRUCT_BLOCK;

 

 

FUNCTION SendData : BOOL;

VAR_INPUT

  v   : ACCESS server_pack;

  res : ACCESS INT;

END_VAR;

 

  ... Sends the data and returns the status in 'res'.

  res := 0;

  SendData := TRUE;

   

END_FUNCTION;

 

VAR

  pack : server_pack;

  rc   : INT;

END_VAR;

 

Calling the function to send the "pack" of data:

 

... Fill in the data to send in 'pack', and send.

SendData( v := pack, res := rc );

 

 

Note that any possible changes to the "v" formal parameter in the SendData function will affect the content of the actual parameter "pack".

 

 

Using ACCESS parameters to a function has many advantages:

 

In addition to the single return value from a function, an arbitrary number of additional return values can be realized.

It can often be used easier and with a memory saving if used instead of a FUNCTION_BLOCK.

ACCESS parameter passing with a STRUCT_BLOCK is possible, which allows a composite type to be passed and returned.

 

.. and only a few rules apply:

 

All ACCESS parameters must be assigned by the caller.

The variable types of the formal parameter and the actual parameter must be identical.

 

 

Also see the section on functions in the VPL Introduction Manual.