Technical data
Basics of Designing a Program Structure
4.2 Blocks in the User Program
Programming with STEP 7
88 Manual, 05/2010, A5E02789666-01
4.2.3.2 Functions (FC)
Functions (FCs) belong to the blocks that you program yourself. A function is a logic block "without
memory." Temporary variables belonging to the FC are saved in the local data stack. This data is
then lost when the FC has been executed. To save data permanently, functions can also use
shared data blocks.
Since an FC does not have any memory of its own, you must always specify actual parameters for
it. You cannot assign initial values for the local data of an FC.
Application
An FC contains a program section that is always executed when the FC is called by a different
logic block. You can use functions for the following purposes:
• To return a function value to the calling block (example: math functions)
• To execute a technological function (example: single control function with a bit logic operation).
Assigning Actual Parameters to the Formal Parameters
A formal parameter is a dummy for the "actual" parameter. Actual parameters replace the formal
parameters when the function is called. You must always assign actual parameters to the formal
parameters of an FC (for example, an actual parameter "I 3.6" to the formal parameter "Start"). The
input, output and in/out parameters used by the FC are saved as pointers to the actual parameters
of the logic block that called the FC.
Important Differences Between the Output Parameters of FCs and FBs
In function blocks (FB), a copy of the actual parameters in the instance DB is used when accessing
the parameters. If an input parameter is not transferred or an output parameter is not write
accessed when a FB is called, the older values still stored in the instance DB /Instance DB =
memory of the FBs) will be used.
Functions (FC) have no memory. Contrary to FBs, the assignment of formal parameters to these
FCs is therefore not optional, but rather essentially. FC parameters are accessed via addresses
(pointers to targets across area boundaries). When an address of the data area (data block) or a
local variable of the calling block is used as actual parameter, a copy of the actual parameter is
saved temporarily to local data area of the calling block for the transfer of the parameter.
Caution
In this case, if no data are written to an OUTPUT parameter in an FC, the block may output random
values!
As the calling block's local data area which is reserved for the copy is not assigned to the OUTPUT
parameter, no data will be written to this area. It will therefore remain unchanged and the random
value stored at this location will be output, because local data are not automatically set to "0" by
default, for example.