Program Control Instructions
17.2 Calling Functions and Function Blocks with CALL
You can use the Call (CALL) instruction to call functions (FCs) and function blocks (FBs) that you have created for your program or that you have received as standard functions and standard function blocks from Siemens.
The Call instruction calls the FC or FB that you indicate as an address regardless of the result of logic operation or any other condition.
When you use the Call instruction to call a function block, you must supply the function block with an instance data block (instance DB) or declare it as a local instance. The instance data block stores all the static variables and actual parameters of the function block.
If you need information on how to program a function or a function block, or how to work with their parameters, see the STEP 7 Online Help.
When calling a function (FC) or a function block (FB), you must assign corresponding actual parameters to the declared formal parameters.
The actual parameter that is specified when a function block is called must have the same data type as the formal parameter.
The actual parameters used when a function (FC) or function block (FB) is called are generally specified symbolically. Absolute addressing of actual parameters is possible only for an address whose maximum size is a double word (for example, I 1.0, MB2, QW4, ID0).
When you call a function, all formal parameters must be supplied with actual parameters. You only need to declare the actual parameters when these are different to the parameters of the previous call (actual parameters remain stored in the instance data block after the processing of the function block has been completed).
When you call a function block, the Call instruction copies one of the following items into the instance data block of the function block, depending on the data type of the actual parameter and on the declaration of the formal parameter (IN, OUT, IN_OUT):
The value of the actual parameter
A pointer to the address of the actual parameter
A pointer to the L stack of the calling block where the value of the actual parameter has been buffered
Description
Formal Parameters and Actual
Parameters
Specifying the Actual Parameters
Program Control Instructions
The call can take place once the following details are entered:
The name of the function block
The name of the instance data block and
The parameters (if the actual parameter is a data block, the complete absolute address must always be specified, for example DB1.DBW2).
The call uses either with an absolute or a symbolic address.
Absolute Call:
CALL FBx,DBy (pass parameters);
x = block number y = data block number Symbolic Call:
CALL fbname,datablockname (pass parameters);
fbname = symbolic block name
datablockname = symbolic data block name
The following example shows the call of function block FB40 with instance data block DB41. In this example, the formal parameters have the following data types:
Call of FB40 with instance data block DB41.
IN1 (formal parameter) is supplied with I 1.0 (actual parameter).
IN2 (formal parameter) is supplied with MW2 (actual parameter).
OUT1 (formal parameter) is supplied with MD20 (actual parameter).
With this instruction, the program accesses the formal parameter OUT1.
The following example shows the call of function block FB50 with instance data block DB51. In this example, the formal parameters have the following data types: Calling an FB with
Instance DB and Block Parameters
Examples
Program Control Instructions
STL Explanation CALL FB50,DB51
IN10:= I1.0 OUT11:= ACTPA11
Call of FB50 with instance data block DB51.
IN10 (formal parameter) is supplied with I 1.0 (actual parameter).
Here it is not possible to specify an absolute actual parameter (for example, MW10) because the formal
parameter OUT11 was defined as a structure. Instead, the symbolic actual parameter ACTPA11 is specified. Please note that ACTPA11 has the same structure as the formal parameter OUT11.
Access to the values of the structure OUT11 in FB50 would be made as follows:
STL Explanation
A OUT11.V1 L OUT11.V2
Perform an AND logic operation on the bit OUT11.V1.
Load word OUT11.V2 into accumulator 1.
The call can take place once the following details are entered:
The instance name (= name of a static variable of the type FB z) and
The Parameters
The call always has a symbolic designation.
CALL Instance Name (pass parameters);
STL Explanation
FUNCTION_BLOCK FB 11 VAR
loc_inst : FB 10;
END_VAR BEGIN NETWORK
CALL #loc_inst ( in_bool := M 0.0);
Source file
Declaration of a multiple instance with data type FB10
Call of the multiple instance with syntax Parameter pass
(in_bool in this case is a variable declared in FB10) Calling Multiple
Instances
Program Control Instructions
The call can take place once the following details are entered:
The name of the fucntion block and
The parameters
The call is addressed either with an absolute or a symbolic name.
Absolute Call:
CALL FCx (pass parameters);
x = block number Symbolic Call:
CALL fcname (pass parameters);
fcname = symbolic block name
The following example shows the call of function FC80 with block parameters. In this example, the formal parameters have the following data types:
INC1: BOOL INC2: INT OUT: WORD
STL Explanation
CALL FC80 INC1:= M 1.0 INC2:= IW2 OUT:= QW4
Call FC80.
INC1 (formal parameter) is supplied with M 1.0 (actual parameter).
INC2 (formal parameter) is supplied with IW2 (actual parameter).
OUT (formal parameter) is supplied with QW4 (actual parameter).
You can create a function (FC) that delivers a return value (RET_VAL). For example, if you want to create a floating-point math function, you can use this return value as an output for the result of your function. When you call this function in your program, you provide the output “RET_VAL” with a double word location to accommodate the 32-bit result of your floating-point math function.
Calling an FC with Block Parameters
Example
Calling an FC That Delivers a Return Value
Program Control Instructions