Reusable code patterns in PLC Programming - Time & Space Complexity
When we use reusable code patterns in PLC programming, we want to know how the time to run the code changes as we use these patterns more.
We ask: How does the execution time grow when the input or usage grows?
Analyze the time complexity of the following reusable function block pattern.
FUNCTION_BLOCK Counter
VAR_INPUT
Increment : BOOL;
END_VAR
VAR_OUTPUT
Count : INT;
END_VAR
IF Increment THEN
Count := Count + 1;
END_IF
END_FUNCTION_BLOCK
This code defines a reusable counter block that increases its count when triggered.
Look for repeated actions in the code.
- Primary operation: The increment check and count update inside the function block.
- How many times: Once per call of the function block, which can be many times in a program cycle.
Each time the function block is called, it does a simple check and possibly increments.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 calls | 10 checks and possible increments |
| 100 calls | 100 checks and possible increments |
| 1000 calls | 1000 checks and possible increments |
Pattern observation: The operations grow directly with the number of calls.
Time Complexity: O(n)
This means the time to run grows in a straight line with how many times the reusable code is called.
[X] Wrong: "Reusable code always runs instantly no matter how many times it is used."
[OK] Correct: Each call still takes time, so more calls mean more total time.
Understanding how reusable code affects time helps you write efficient programs and explain your design choices clearly.
"What if the reusable code called itself recursively? How would the time complexity change?"
Practice
FUNCTION_BLOCK in PLC programming?Solution
Step 1: Understand the purpose of FUNCTION_BLOCK
FUNCTION_BLOCK is designed to package code that can be reused multiple times in a PLC program.Step 2: Identify the main benefit
Reusing code saves time and makes the program easier to manage and maintain.Final Answer:
It allows you to reuse code and simplify program management. -> Option DQuick Check:
Reusable code = Simplify management [OK]
- Thinking FUNCTION_BLOCK speeds up execution
- Believing it fixes errors automatically
- Confusing it with user interface creation
Counter in PLC programming?Solution
Step 1: Identify correct FUNCTION_BLOCK syntax
FUNCTION_BLOCK declaration starts with 'FUNCTION_BLOCK Name' and ends with 'END_FUNCTION_BLOCK'. Inputs are declared inside VAR_INPUT.Step 2: Check each option
FUNCTION_BLOCK Counter VAR_INPUT Count: INT; END_VAR END_FUNCTION_BLOCK correctly declares FUNCTION_BLOCK Counter with VAR_INPUT and ends properly. FUNCTION Counter VAR_INPUT Count: INT; END_VAR END_FUNCTION uses FUNCTION instead of FUNCTION_BLOCK. FUNCTION_BLOCK Counter() VAR_INPUT Count: INT; END_VAR END_FUNCTION_BLOCK adds parentheses which are not used. FUNCTION_BLOCK Counter VAR_OUTPUT Count: INT; END_VAR END_FUNCTION_BLOCK uses VAR_OUTPUT instead of VAR_INPUT for input variable.Final Answer:
FUNCTION_BLOCK Counter VAR_INPUT Count: INT; END_VAR END_FUNCTION_BLOCK -> Option BQuick Check:
Correct FUNCTION_BLOCK syntax = FUNCTION_BLOCK Counter VAR_INPUT Count: INT; END_VAR END_FUNCTION_BLOCK [OK]
- Using FUNCTION instead of FUNCTION_BLOCK
- Adding parentheses after FUNCTION_BLOCK name
- Declaring inputs under VAR_OUTPUT
Result after execution?FUNCTION_BLOCK AddTwo VAR_INPUT A: INT; B: INT; END_VAR VAR_OUTPUT Result: INT; END_VAR Result := A + B; END_FUNCTION_BLOCK // Usage AddTwoInstance: AddTwo; AddTwoInstance.A := 3; AddTwoInstance.B := 5; // Execute block // What is AddTwoInstance.Result?
Solution
Step 1: Understand the FUNCTION_BLOCK logic
The block adds inputs A and B and stores the sum in Result.Step 2: Substitute input values and calculate
A = 3, B = 5, so Result = 3 + 5 = 8.Final Answer:
8 -> Option AQuick Check:
3 + 5 = 8 [OK]
- Concatenating numbers as strings
- Assuming Result is zero by default
- Thinking Result is not assigned automatically
FUNCTION_BLOCK Multiply VAR_INPUT X: INT; Y: INT; END_VAR VAR_OUTPUT Product: INT; END_VAR Product = X * Y; END_FUNCTION_BLOCK
Solution
Step 1: Check assignment syntax in FUNCTION_BLOCK
In PLC programming, assignment uses ':=' not '='.Step 2: Verify other syntax elements
Semicolon is present. VAR_INPUT before VAR_OUTPUT is correct. FUNCTION_BLOCK names are case-sensitive but uppercase is allowed.Final Answer:
Incorrect use of '=' instead of ':=' for assignment. -> Option AQuick Check:
Use ':=' for assignment [OK]
- Using '=' instead of ':='
- Thinking semicolon is optional
- Believing VAR_OUTPUT must come first
Trigger changes from FALSE to TRUE. Which approach correctly implements this behavior?Solution
Step 1: Understand the counting condition
The counter should increase only when Trigger changes from FALSE to TRUE (rising edge).Step 2: Implement edge detection logic
Store previous Trigger state in a VAR variable. Compare current and previous states. Increment counter only if previous was FALSE and current is TRUE.Final Answer:
Use a VAR to store previous Trigger state and increment a counter only when Trigger changes from FALSE to TRUE. -> Option CQuick Check:
Edge detection needs previous state storage [OK]
- Incrementing counter every cycle
- Resetting counter incorrectly
- Using VAR_INPUT for internal state
Ready to prove it?
Pass the test and this pattern moves unknown
