Program organization units (POU) in PLC Programming - Time & Space Complexity
When we use program organization units (POUs) in PLC programming, we want to know how the program's running time changes as we add more units or calls.
We ask: How does the execution time grow when the program calls multiple POUs?
Analyze the time complexity of the following code snippet.
PROGRAM MainProgram
VAR
i : INT;
subInst : SubProgram;
END_VAR
FOR i := 1 TO 10 DO
subInst();
END_FOR
END_PROGRAM
FUNCTION_BLOCK SubProgram
VAR
x : INT;
END_VAR
x := x + 1;
END_FUNCTION_BLOCK
This code calls a subprogram 10 times inside a loop. The subprogram does a simple increment.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop in MainProgram that calls SubProgram 10 times.
- How many times: The subprogram is called once per loop iteration, so 10 times total.
As the number of loop iterations increases, the total calls to the subprogram increase at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to SubProgram |
| 100 | 100 calls to SubProgram |
| 1000 | 1000 calls to SubProgram |
Pattern observation: The total work grows directly with the number of calls; doubling the calls doubles the work.
Time Complexity: O(n)
This means the program's running time grows linearly with the number of POU calls.
[X] Wrong: "Calling a POU inside a loop does not add to execution time because it's just a call."
[OK] Correct: Each call runs the POU code, so more calls mean more work and longer execution time.
Understanding how calling POUs affects execution time shows you can reason about program structure and performance, a useful skill in real automation projects.
What if the SubProgram called another POU inside itself? How would the time complexity change?
Practice
Solution
Step 1: Recall POU types
The three main POUs are PROGRAM, FUNCTION_BLOCK, and FUNCTION.Step 2: Identify the incorrect option
MODULE is not a standard POU type in PLC programming.Final Answer:
MODULE -> Option AQuick Check:
POUs = PROGRAM, FUNCTION_BLOCK, FUNCTION [OK]
- Confusing MODULE with FUNCTION_BLOCK
- Thinking FUNCTION_BLOCK is not a POU
- Mixing hardware modules with POUs
MotorControl in IEC 61131-3 standard?Solution
Step 1: Identify correct POU declaration syntax
FUNCTION_BLOCK declaration starts with 'FUNCTION_BLOCK Name' and ends with 'END_FUNCTION_BLOCK'.Step 2: Check each option
FUNCTION_BLOCK MotorControl END_FUNCTION_BLOCK matches correct syntax; PROGRAM and FUNCTION declare different POUs; the version with parentheses is invalid.Final Answer:
FUNCTION_BLOCK MotorControl\nEND_FUNCTION_BLOCK -> Option CQuick Check:
FUNCTION_BLOCK syntax = FUNCTION_BLOCK MotorControl END_FUNCTION_BLOCK [OK]
- Adding parentheses to FUNCTION_BLOCK
- Confusing FUNCTION and FUNCTION_BLOCK syntax
- Using PROGRAM keyword for function blocks
FUNCTION AddTwo : INT VAR_INPUT a : INT; b : INT; END_VAR AddTwo := a + b; END_FUNCTION
What is the output of calling
AddTwo(3, 5)?Solution
Step 1: Understand FUNCTION behavior
The FUNCTION AddTwo takes two INT inputs and returns their sum.Step 2: Calculate the output for inputs 3 and 5
3 + 5 = 8, so the function returns 8.Final Answer:
8 -> Option AQuick Check:
3 + 5 = 8 [OK]
- Concatenating inputs as strings
- Thinking FUNCTION_BLOCKs return values
- Assuming syntax error on call
FUNCTION_BLOCK Timer VAR StartTime : TIME; END_VAR METHOD Start() StartTime := TIME(); END_METHOD END_FUNCTION_BLOCK
Solution
Step 1: Check METHOD usage in FUNCTION_BLOCK
METHODs are allowed inside FUNCTION_BLOCKs to define actions.Step 2: Verify TIME() usage
TIME() is not a standard function to get current time in PLC; this causes an error.Final Answer:
TIME() is not a valid way to get current time -> Option BQuick Check:
Invalid TIME() call = TIME() is not a valid way to get current time [OK]
- Assuming METHODs are disallowed
- Thinking all variables must be inputs
- Believing FUNCTION_BLOCKs can't have variables
Solution
Step 1: Understand POU types and their features
FUNCTION_BLOCKs can hold internal data/state and be instantiated multiple times.Step 2: Compare with other POUs
FUNCTIONs return values but do not hold state; PROGRAMs organize code but are not reusable units with state.Final Answer:
FUNCTION_BLOCK, because it can hold data and be reused with different instances -> Option DQuick Check:
Reusable with state = FUNCTION_BLOCK [OK]
- Choosing FUNCTION for holding state
- Confusing PROGRAM with reusable unit
- Ignoring difference between FUNCTION and FUNCTION_BLOCK
Ready to prove it?
Pass the test and this pattern moves unknown
