In the process of programming Siemens PLCs, it is common to find both FB blocks and FC blocks in a program. So, what are the differences between the two?
01What are FB and FC blocks, and how to use them
FB and FC are essentially equivalent to subroutines.
First, let’s introduce subroutines: Subroutines and main programs refer to segments of a program. During the design of a program, it is common to encounter segments of code that perform the same function, such as lighting up a lamp in a specific logical sequence.
Such segments are not only difficult to maintain but also prone to errors, making the program bulky. To overcome this drawback, when encountering segments with the same functionality, they can be handled as subroutines. A subroutine is a segment of code that has specific functionality and logical integrity; it exists independently but can only serve a particular program. Using subroutines simplifies program writing and enhances readability and reusability.
02Differences Between FB and FC
FB – Function Block, with background data block; FC – Function, equivalent to a function. Both FB and FC blocks are equivalent to subroutines, capable of calling other FB and FC blocks, and can also be called by OB, FB, and FC blocks.
Differences:
-
FB uses a background data block as a storage area, while FC does not have an independent storage area and uses global DB or M area.
-
FB local variables include STAT and TEMP, while FC does not have its own storage area and therefore does not possess STAT; TEMP cannot be initialized.
-
Essentially, the purpose of FB and FC is the same; regardless of the logical requirements, both can be implemented. The efficiency of implementation varies, which also relates to the individual programming habits of engineers.
Advantages of FB blocks:
-
Easy portability; for controlled objects with the same control logic but different parameters, the same FB block can be conveniently used with different background DBs.
-
Multiple backgrounds reduce repetitive work and improve efficiency.
-
Convenient parameter modification during multiple calls.
-
Has an independent storage area.
Advantages of FC blocks:
-
Compact and flexible, easier to understand for programs that are not called multiple times.
-
Does not occupy additional storage resources.
03Pin Definitions for FB and FC Blocks
FB:

FC:

Input
Variables are externally input and can only be read by this program block, not written by it.
Output is output from this program block; it can be read and written by this program block, while other programs can only read the value through the pin and cannot write.。
IN_OUT
Input-output variables can be read and written by both this program block and other programs.。
TEMP
Temporary variables, as the name suggests, are variables used for temporarily storing data. These temporary data are stored in the local data stack (L stack) of the CPU working memory.
Static
Static variables are always stored during the PLC operation. S7 defines static variables in the background data block (only for FB; FC and OB do not have static variables). When the called block runs, it can read or modify static variables; after the called block ends, the static variables remain in the data block.
04Problems Caused by Temporary Variables
Temporary variables can be used in organizational blocks OB, function FC, and function blocks FB. When the block executes, they are used to temporarily store data. Once the block execution ends, the stack address will be reallocated for use by other program blocks, and the data at this address will not be cleared until assigned a new value by another program block. It is necessary to follow the principle of “assign first, then use”.
Therefore, there are several common situations that lead to abnormal program operation:
1. A certain block program runs inconsistently, where one or more values occasionally behave abnormally. This issue arises from not adhering to the principle of “assign first, then use”. Otherwise, the TEMP value at the start of each scan cycle will be undefined, and the value at this address will be random.
2. Multiple blocks use TEMP, and any one of them works fine individually, but they cannot work together. This issue arises because TEMP was not assigned before use; the TEMP value in program block 1 was not cleared, and the CPU’s operation mechanism called this address for use or directly assigned it to program block 2, causing this TEMP address to not be zero, leading to program confusion. Since the memory operation mechanism is not transparent, this allocation process appears random. This may cause the program to run correctly multiple times but encounter issues after running for a while. As long as the principle of “assign first, then use” is followed, this can be avoided.
3. TEMP cannot achieve self-locking. This issue arises because TEMP values cannot retain the previous cycle’s value like M points or Q points; TEMP needs to have a clear assignment in each scan cycle, i.e., assign first (write), then use (read/write). The solution is to use STAT static variables in FB; FC can use M area or global DB addresses.
Summary:
When using temporary variables TEMP:1. Do not use before assigning.2. Not suitable for self-locking coils.3. Not suitable for rising or falling edges.。
In case of the above situations
FC blocks can use M area or global DB addresses; FB blocks can also use their own background DB’s STAT static variables.
When using a temporary variable that is called for the first time in FB or FC, it must be assigned first (write instruction), not read instruction.Now let’s look at the differences during programming:
FB: Function blocks are code blocks that permanently store their values in the background data block, so these values remain available after the block execution. When we call a function blockFB, a background data block is automatically generated, as shown in the following image:

FC: Functions are code blocks without dedicated storage areas. This means that when callingFC blocks, actual parameters must be assigned to each formal parameter to run.

We create two program blocksFB and FC to observe the differences in their ports:

From the image, we can see that both haveINPUT, OUTPUT, INOUT, TEMP interfaces.
FB has an additionalstatic: static variable,
and each interface has initial values that can be directly read by the human-machine interface, whileFC interfaces cannot be directly read by the human-machine interface and require global variables to be set.
Next, we will demonstrate the differences in usage with a simple forward and reverse program. To better compare, we will set the programs to be the same, as shown in the following image:

FB

Then, when called in the main programMAIN, it is found thatFB does not report an error even if the parameters for each interface are not assigned, whileFC reports an error when the parameters for each interface are not assigned.
FB automatically generates a dedicated background data blockFB_1_DB, whileFC does not have a dedicated background data block, as shown in the following image:

Next, we will look at the running situation after assigning actual parameters to the interfaces ofFB and FC. As shown in the following image:
