Programming concepts
7.3 Using blocks to structure your program
Block optimization and passing parameters
The user program passes FC parameters as "call-by-value" for simple data types (for
example, INT, DINT, and REAL). It passes complex data types (for example, STRUCT,
ARRAY, and STRING) as "call-by-reference".
The user program normally passes FB parameters in the instance Data block (DB)
associated with the FB:
● The user program passes simple data types (for example, INT, DINT, and REAL) as "call-
by-value" by copying the parameters to/from the instance DB.
● The user program copies complex data types (for example, STRUCT, ARRAY, and
STRING) to and from the instance DB for IN and OUT parameter types.
● The user program passes complex data types as "call-by-reference" for the IN/OUT
interface type.
DBs can be created as either "Optimized" or "Standard" (non-optimized). The optimized data
blocks are more compact than the non-optimized data blocks. Also, the ordering of the data
elements within the DB is different for optimized versus non-optimized DBs. Refer to the
"Optimized blocks" section of the S7-Programming Guideline for S7-1200/1500, STEP 7 (TIA
Portal), 03/2014 (http://support.automation.siemens.com/WW/view/en/81318674) for a
discussion of optimized blocks.
You create FBs and FCs to process either optimized or non-optimized data. You can select
the "Optimized block access" check box as one of the attributes for the block. The user
program optimizes program blocks by default, and the program blocks expect data passed to
the block to be in the optimized format.
When the user program passes a complex parameter (for example, a STRUCT) to a
function, the system checks the optimization setting of the data block containing the structure
and the optimization setting of the program block. If you optimize both the data block and the
function, then the user program passes the STRUCT as a "call-by-reference". The same is
true if you select non-optimized for both the data block and the function.
However, if you make the function and data block optimization different (meaning that you
optimized one block and not the other block), the STRUCT must be converted to the format
expected by the function. For example, if you select non-optimized for the data block and
optimized for the function, then a STRUCT in the data block must be converted to an
optimized format before the function can process the STRUCT. The system does this
conversion by making a "copy" of the STRUCT and converting it to the optimized format that
the function expects.
In summary, when the user program passes a complex data type (for example, a STRUCT)
to a function as an IN/OUT parameter, the function expects the user program to pass the
STRUCT as a "call-by-reference":
● If you select optimized or non-optimized for both the data block containing the STRUCT
and the function, the user program passes the data as "call-by-reference".
● If you do not configure the data block and the function with the same optimization settings
(one is optimized and the other is non-optimized), the system must make a copy of the
STRUCT before passing it to the function. Because the system has to make this copy of
the structure, this converts the "call-by-reference", effectively, into a "call-by-value".
196
S7-1200 Programmable controller
System Manual, V4.2, 09/2016, A5E02486680-AK