Bird
Raised Fist0
PLC Programmingscripting~10 mins

Function block instances in PLC Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Function block instances
Define Function Block
↓
Create Instance 1
→Instance 1 has own memory
↓
Create Instance 2
→Instance 2 has own memory
↓
Call Instance 1
→Executes with Instance 1 data
↓
Call Instance 2
→Executes with Instance 2 data
↓
Results stored separately in each instance
Function blocks are like mini-programs with their own memory. Each instance keeps its own data, so calling one does not affect the other.
Execution Sample
PLC Programming
FUNCTION_BLOCK Counter
VAR
  count : INT := 0;
END_VAR
count := count + 1;
END_FUNCTION_BLOCK

// Create two instances
Counter1 : Counter;
Counter2 : Counter;

// Call instances
Counter1();
Counter2();
Counter1();
This code defines a counter function block, creates two instances, and calls them to increment their own counts separately.
Execution Table
StepActionInstance1 countInstance2 countExplanation
1Create Instance10-Instance1 count initialized to 0
2Create Instance200Instance2 count initialized to 0
3Call Instance1()10Instance1 increments count to 1
4Call Instance2()11Instance2 increments count to 1
5Call Instance1()21Instance1 increments count to 2
6End21Execution stops after calls
💡 All calls executed; each instance maintains its own count separately.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
Instance1.count01122
Instance2.count-0111
Key Moments - 2 Insights
Why does Instance1.count change but Instance2.count does not when calling Instance1()?
Each instance has its own memory. Calling Instance1() only changes Instance1.count, as shown in execution_table rows 3 and 5.
Why is Instance2.count initialized to 0 only after creating Instance2?
Each instance initializes its variables separately when created, so Instance2.count starts at 0 at step 2 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is Instance1.count after step 4?
A2
B0
C1
DUndefined
💡 Hint
Check the 'Instance1 count' column at step 4 in the execution_table.
At which step does Instance2.count first increase?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Instance2 count' column in execution_table rows 3 and 4.
If we call Instance2() again after step 5, what will Instance2.count be?
A2
B1
C3
D0
💡 Hint
Instance2.count increments by 1 each call; see variable_tracker for pattern.
Concept Snapshot
Function Block Instances:
- Define a function block with variables.
- Create multiple instances; each has separate memory.
- Calling an instance runs code using its own data.
- Changes in one instance do not affect others.
- Useful for reusable, independent components.
Full Transcript
Function block instances in PLC programming allow you to create multiple copies of a function block, each with its own separate memory. When you define a function block, you write code with variables inside it. Creating an instance means making a copy that keeps its own variable values. Calling an instance runs the function block code using that instance's data only. For example, if you have a counter function block, each instance counts separately. This is shown in the execution table where Instance1 and Instance2 counts increase independently. This helps keep data separate and organized when using the same logic multiple times.

Practice

(1/5)
1. What is the main purpose of using function block instances in PLC programming?
easy
A. To reuse code with separate memory for each instance
B. To write code that runs only once
C. To combine multiple programs into one file
D. To create global variables accessible everywhere

Solution

  1. Step 1: Understand function block instances

    Function block instances allow you to reuse the same code but keep data separate for each instance.
  2. Step 2: Compare options

    Only To reuse code with separate memory for each instance correctly describes this purpose. Other options describe unrelated concepts.
  3. Final Answer:

    To reuse code with separate memory for each instance -> Option A
  4. Quick Check:

    Function block instances = separate memory reuse [OK]
Hint: Think: same code, different data for each instance [OK]
Common Mistakes:
  • Confusing instances with global variables
  • Thinking instances run only once
  • Mixing instances with program files
2. Which of the following is the correct syntax to declare two separate instances of a function block named TimerFB in IEC 61131-3 style?
easy
A. Timer1 -> TimerFB; Timer2 -> TimerFB;
B. Timer1 : TimerFB; Timer2 : TimerFB;
C. Timer1 = TimerFB(); Timer2 = TimerFB();
D. Timer1 : TimerFB() Timer2 : TimerFB()

Solution

  1. Step 1: Recall IEC 61131-3 instance declaration

    Instances are declared with name : FunctionBlockName; syntax.
  2. Step 2: Check options

    Timer1 : TimerFB; Timer2 : TimerFB; uses correct syntax with semicolons. Others use invalid or incorrect syntax.
  3. Final Answer:

    Timer1 : TimerFB; Timer2 : TimerFB; -> Option B
  4. Quick Check:

    Instance declaration uses colon and semicolon [OK]
Hint: Use colon and semicolon to declare instances [OK]
Common Mistakes:
  • Using equal sign instead of colon
  • Omitting semicolons
  • Adding parentheses after function block name
3. Given the following code snippet in a PLC program:
Timer1 : TimerFB;
Timer2 : TimerFB;

Timer1(IN := TRUE);
Timer2(IN := FALSE);

// After one cycle, what is the state of Timer1.Q and Timer2.Q?
medium
A. Timer1.Q = TRUE, Timer2.Q = FALSE
B. Timer1.Q = FALSE, Timer2.Q = TRUE
C. Timer1.Q = TRUE, Timer2.Q = TRUE
D. Timer1.Q = FALSE, Timer2.Q = FALSE

Solution

  1. Step 1: Understand instance independence

    Timer1 and Timer2 are separate instances with their own inputs and outputs.
  2. Step 2: Analyze input signals

    Timer1.IN is TRUE, so Timer1.Q will become TRUE after timing. Timer2.IN is FALSE, so Timer2.Q remains FALSE.
  3. Final Answer:

    Timer1.Q = TRUE, Timer2.Q = FALSE -> Option A
  4. Quick Check:

    Separate instances keep outputs independent [OK]
Hint: Each instance acts independently with its own inputs [OK]
Common Mistakes:
  • Assuming outputs share state between instances
  • Confusing input signals for both instances
  • Ignoring instance separation
4. Identify the error in this function block instance declaration:
Timer1 : TimerFB
Timer2 : TimerFB;
medium
A. Incorrect function block name
B. Extra semicolon after Timer2 declaration
C. Using colon instead of equal sign
D. Missing semicolon after Timer1 declaration

Solution

  1. Step 1: Check syntax for instance declarations

    Each instance declaration must end with a semicolon.
  2. Step 2: Locate the missing semicolon

    Timer1 declaration lacks a semicolon at the end, causing syntax error.
  3. Final Answer:

    Missing semicolon after Timer1 declaration -> Option D
  4. Quick Check:

    Each declaration ends with semicolon [OK]
Hint: Always end instance declarations with semicolon [OK]
Common Mistakes:
  • Omitting semicolon after each instance
  • Confusing colon with equal sign
  • Assuming semicolon is optional
5. You want to control three independent motors using the same function block MotorControlFB. Which approach correctly uses function block instances to achieve this?
hard
A. Create three copies of MotorControlFB code with different names
B. Declare one instance MotorControlFB and call it three times with different inputs
C. Declare three instances: Motor1, Motor2, Motor3 of MotorControlFB and call each separately
D. Use global variables inside MotorControlFB to handle all three motors

Solution

  1. Step 1: Understand instance independence for multiple devices

    Each motor needs its own instance to keep control data separate.
  2. Step 2: Evaluate options

    Declare three instances: Motor1, Motor2, Motor3 of MotorControlFB and call each separately declares three separate instances, allowing independent control. Declare one instance MotorControlFB and call it three times with different inputs reuses one instance incorrectly. Use global variables inside MotorControlFB to handle all three motors uses globals, which mixes data. Create three copies of MotorControlFB code with different names duplicates code, which is inefficient.
  3. Final Answer:

    Declare three instances: Motor1, Motor2, Motor3 of MotorControlFB and call each separately -> Option C
  4. Quick Check:

    Multiple devices = multiple instances [OK]
Hint: One instance per device keeps data separate [OK]
Common Mistakes:
  • Trying to reuse one instance for multiple devices
  • Using global variables inside function block
  • Copy-pasting code instead of using instances
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard