Bird
Raised Fist0
PLC Programmingscripting~20 mins

Function block definition in PLC Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Function Block Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a simple function block call
What is the output of the following function block execution?
PLC Programming
FUNCTION_BLOCK FB_Add
VAR_INPUT
  A : INT;
  B : INT;
END_VAR
VAR_OUTPUT
  Sum : INT;
END_VAR

Sum := A + B;
END_FUNCTION_BLOCK

PROGRAM PLC_PRG
VAR
  fbAddInstance : FB_Add;
  result : INT;
END_VAR

fbAddInstance(A := 3, B := 4);
result := fbAddInstance.Sum;
A34
B7
C0
DError: Missing method call
Attempts:
2 left
💡 Hint
Remember that the function block calculates Sum as A + B.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in function block definition
Which option contains the correct syntax for defining a function block with one input and one output?
A
FUNCTION_BLOCK FB_Mult
VAR_INPUT
  X : INT;
END_VAR
VAR_OUTPUT
  Y : INT;
END_VAR
Y := X * 2;
END_FUNCTION_BLOCK
B
FUNCTION_BLOCK FB_Mult
VAR_INPUT
  X : INT
END_VAR
VAR_OUTPUT
  Y : INT;
END_VAR
Y := X * 2;
END_FUNCTION_BLOCK
C
FUNCTION_BLOCK FB_Mult
VAR_INPUT
  X : INT;
END_VAR
VAR_OUTPUT
  Y : INT
END_VAR
Y := X * 2;
END_FUNCTION_BLOCK
D
FUNCTION_BLOCK FB_Mult
VAR_INPUT
  X : INT;
END_VAR
VAR_OUTPUT
  Y : INT;
END_VAR
Y = X * 2;
END_FUNCTION_BLOCK
Attempts:
2 left
💡 Hint
Check for missing semicolons and assignment operator correctness.
🔧 Debug
advanced
2:00remaining
Find the runtime error in this function block usage
What runtime error will occur when executing this program?
PLC Programming
FUNCTION_BLOCK FB_Divide
VAR_INPUT
  Numerator : INT;
  Denominator : INT;
END_VAR
VAR_OUTPUT
  Result : REAL;
END_VAR

IF Denominator = 0 THEN
  Result := -1.0; (* error code for division by zero *)
ELSE
  Result := Numerator / Denominator;
END_IF
END_FUNCTION_BLOCK

PROGRAM PLC_PRG
VAR
  fbDiv : FB_Divide;
  output : REAL;
END_VAR

fbDiv(Numerator := 10, Denominator := 0);
output := fbDiv.Result;
ADivision by zero runtime error
BSyntax error due to missing END_IF
CNo runtime error; output is -1.0
DOutput is 0.0 due to integer division
Attempts:
2 left
💡 Hint
Check how the function block handles zero denominator.
🚀 Application
advanced
2:00remaining
Determine the final output after multiple function block calls
Given the function block and program below, what is the value of 'finalResult' after execution?
PLC Programming
FUNCTION_BLOCK FB_Accumulator
VAR_INPUT
  AddValue : INT;
END_VAR
VAR_OUTPUT
  Total : INT := 0;
END_VAR

Total := Total + AddValue;
END_FUNCTION_BLOCK

PROGRAM PLC_PRG
VAR
  acc : FB_Accumulator;
  finalResult : INT;
END_VAR

acc(AddValue := 5);
acc(AddValue := 3);
acc(AddValue := 2);
finalResult := acc.Total;
A2
B10
C0
D5
Attempts:
2 left
💡 Hint
Consider how the Total variable is initialized and updated each call.
🧠 Conceptual
expert
2:00remaining
Understanding persistent state in function blocks
Which statement correctly explains how to maintain state across multiple calls of a function block in PLC programming?
ADeclare variables inside VAR_INPUT to keep their values between calls.
BUse VAR_OUTPUT variables to store state because outputs persist automatically.
CUse global variables outside the function block to maintain state.
DDeclare variables inside VAR to retain their values between calls of the function block instance.
Attempts:
2 left
💡 Hint
Think about where persistent data is stored inside a function block.

Practice

(1/5)
1. What is the main purpose of a Function Block in PLC programming?
easy
A. To group inputs, outputs, and code for repeated tasks
B. To store data permanently in the PLC
C. To create a graphical user interface
D. To connect the PLC to external devices

Solution

  1. Step 1: Understand the role of function blocks

    Function blocks group inputs, outputs, and code to perform tasks repeatedly in PLC programs.
  2. Step 2: Compare options with the definition

    Only To group inputs, outputs, and code for repeated tasks describes grouping inputs, outputs, and code for reuse, which matches the function block purpose.
  3. Final Answer:

    To group inputs, outputs, and code for repeated tasks -> Option A
  4. Quick Check:

    Function block purpose = Group inputs, outputs, and code [OK]
Hint: Function blocks bundle inputs, outputs, and code for reuse [OK]
Common Mistakes:
  • Confusing function blocks with data storage
  • Thinking function blocks create user interfaces
  • Assuming function blocks connect hardware directly
2. Which of the following is the correct way to start defining a function block named MotorControl in IEC 61131-3 structured text?
easy
A. PROGRAM MotorControl
B. FUNCTION MotorControl
C. VAR MotorControl
D. FUNCTION_BLOCK MotorControl

Solution

  1. Step 1: Recall syntax for function block definition

    In IEC 61131-3, function blocks start with the keyword FUNCTION_BLOCK followed by the name.
  2. Step 2: Check each option

    FUNCTION_BLOCK MotorControl uses FUNCTION_BLOCK MotorControl, which is correct. Options A, B, and D use other keywords for different constructs.
  3. Final Answer:

    FUNCTION_BLOCK MotorControl -> Option D
  4. Quick Check:

    Function block syntax = FUNCTION_BLOCK Name [OK]
Hint: Function blocks start with FUNCTION_BLOCK keyword [OK]
Common Mistakes:
  • Using FUNCTION instead of FUNCTION_BLOCK
  • Confusing PROGRAM with function block
  • Using VAR keyword to define function blocks
3. Given this function block code snippet, what will be the output of FB1.Output after calling FB1.Run() twice?
FUNCTION_BLOCK FB1
VAR_INPUT
  In: BOOL;
END_VAR
VAR_OUTPUT
  Output: INT;
END_VAR
VAR
  Counter: INT := 0;
END_VAR

METHOD PUBLIC Run
IF In THEN
  Counter := Counter + 1;
END_IF
Output := Counter;
END_METHOD

END_FUNCTION_BLOCK

// Usage:
FB1.In := TRUE;
FB1.Run();
FB1.Run();
medium
A. 2
B. 1
C. 0
D. TRUE

Solution

  1. Step 1: Understand the counter increment logic

    Each call to Run checks if In is TRUE, then increments Counter by 1.
  2. Step 2: Trace two calls with In = TRUE

    First call increments Counter from 0 to 1, second call increments from 1 to 2. Output is set to Counter.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Counter increments twice with In=TRUE = 2 [OK]
Hint: Count increments each Run call if input TRUE [OK]
Common Mistakes:
  • Assuming Output resets each call
  • Confusing BOOL and INT output types
  • Forgetting Counter is retained between calls
4. Identify the error in this function block definition:
FUNCTION_BLOCK FB_Error
VAR_INPUT
  Start: BOOL;
END_VAR
VAR_OUTPUT
  Status: BOOL;
END_VAR
VAR
  Count: INT;
END_VAR

METHOD Run
IF Start THEN
  Count := Count + 1
  Status := TRUE;
END_IF
END_METHOD
END_FUNCTION_BLOCK
medium
A. Function block name cannot contain underscore
B. Missing semicolon after Count := Count + 1
C. Output variable Status should be input
D. Incorrect keyword METHOD used

Solution

  1. Step 1: Check syntax inside the method

    The line Count := Count + 1 is missing a semicolon at the end, which is required in structured text.
  2. Step 2: Verify other parts

    METHOD keyword is valid, output variable can be named Status, and underscores are allowed in names.
  3. Final Answer:

    Missing semicolon after Count := Count + 1 -> Option B
  4. Quick Check:

    Semicolon missing after assignment line [OK]
Hint: Check each line ends with semicolon in structured text [OK]
Common Mistakes:
  • Ignoring missing semicolons
  • Thinking METHOD keyword is invalid
  • Assuming variable naming rules are stricter
5. You want to create a function block TimerFB that counts seconds when Enable input is TRUE and resets when Reset input is TRUE. Which internal variable type should you use to store the count so it keeps its value between calls?
hard
A. VAR_INPUT
B. VAR_OUTPUT
C. VAR (internal variable)
D. VAR_TEMP

Solution

  1. Step 1: Understand variable types in function blocks

    VAR_INPUT and VAR_OUTPUT are for inputs and outputs, not for storing internal state. VAR_TEMP is temporary and does not keep value between calls.
  2. Step 2: Identify variable type that retains value

    VAR variables inside function blocks keep their value between calls, acting like memory.
  3. Final Answer:

    VAR (internal variable) -> Option C
  4. Quick Check:

    Use VAR for internal memory in function blocks [OK]
Hint: Use VAR for internal memory that persists between calls [OK]
Common Mistakes:
  • Using VAR_TEMP which resets each call
  • Confusing inputs/outputs with internal storage
  • Assuming VAR_INPUT can store internal state
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