Bird
Raised Fist0
PLC Programmingscripting~20 mins

Reusable code patterns in PLC Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PLC Reusable Code Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of a Function Block Instance
Given the following PLC code snippet using a function block instance, what will be the value of the output variable Result after execution?
PLC Programming
FUNCTION_BLOCK FB_Add
VAR_INPUT
  A : INT;
  B : INT;
END_VAR
VAR_OUTPUT
  Result : INT;
END_VAR
Result := A + B;
END_FUNCTION_BLOCK

PROGRAM PLC_PRG
VAR
  Add1 : FB_Add;
  OutputValue : INT;
END_VAR

Add1(A := 5, B := 7);
OutputValue := Add1.Result;
AOutputValue = 5
BOutputValue = 7
COutputValue = 12
DOutputValue = 0
Attempts:
2 left
💡 Hint
Remember that the function block adds inputs A and B and stores the result in Result.
📝 Syntax
intermediate
2:00remaining
Correct Syntax for a Reusable Function
Which of the following code snippets correctly defines a reusable function named Multiply that takes two INT inputs and returns their product as INT?
A
FUNCTION Multiply : INT
VAR_INPUT
  X : INT;
  Y : INT;
END_VAR
RETURN X * Y;
END_FUNCTION
B
FUNCTION Multiply
VAR_INPUT
  X, Y : INT;
END_VAR
RETURN X * Y;
END_FUNCTION
C
FUNCTION Multiply : INT
VAR_INPUT
  X : INT;
  Y : INT;
END_VAR
Multiply = X * Y;
END_FUNCTION
D
FUNCTION Multiply : INT
VAR_INPUT
  X : INT;
  Y : INT;
END_VAR
Multiply := X * Y;
END_FUNCTION
Attempts:
2 left
💡 Hint
Look for correct function declaration and assignment of the return value.
🔧 Debug
advanced
2:00remaining
Identify the Error in Reusable Code Pattern
What error will occur when compiling the following reusable function code?
PLC Programming
FUNCTION FB_Divide : INT
VAR_INPUT
  Numerator : INT;
  Denominator : INT;
END_VAR
IF Denominator = 0 THEN
  FB_Divide := 0;
ELSE
  FB_Divide := Numerator / Denominator;
END_IF
END_FUNCTION
ASyntaxError due to missing semicolon after division operation
BRuntime error: division by zero
CNo error, function runs correctly
DSyntaxError due to missing END_IF keyword
Attempts:
2 left
💡 Hint
Check each line for proper punctuation and keywords.
🚀 Application
advanced
2:00remaining
Using a Reusable Function Block for Temperature Conversion
You have a function block FB_CelsiusToFahrenheit that converts Celsius to Fahrenheit. Which code snippet correctly uses this function block to convert 25°C and store the result in TempF?
PLC Programming
FUNCTION_BLOCK FB_CelsiusToFahrenheit
VAR_INPUT
  TempC : REAL;
END_VAR
VAR_OUTPUT
  TempF : REAL;
END_VAR
TempF := (TempC * 9.0 / 5.0) + 32.0;
END_FUNCTION_BLOCK
A
VAR
  Converter : FB_CelsiusToFahrenheit;
  TempF : REAL;
END_VAR
Converter.TempC := 25.0;
TempF := Converter.TempF;
B
VAR
  Converter : FB_CelsiusToFahrenheit;
  TempF : REAL;
END_VAR
Converter(TempC := 25.0);
TempF := Converter.TempF;
C
VAR
  Converter : FB_CelsiusToFahrenheit;
  TempF : REAL;
END_VAR
Converter(TempC = 25.0);
TempF := Converter.TempF;
D
VAR
  Converter : FB_CelsiusToFahrenheit;
  TempF : REAL;
END_VAR
Converter.TempC = 25.0;
TempF := Converter.TempF;
Attempts:
2 left
💡 Hint
Remember to call the function block with parentheses and assign inputs correctly.
🧠 Conceptual
expert
2:00remaining
Understanding Reusability in PLC Code
Which statement best explains why reusable code patterns like function blocks and functions are important in PLC programming?
AThey reduce code duplication and make maintenance easier by encapsulating logic that can be used multiple times.
BThey increase the size of the program by adding unnecessary layers of code.
CThey make the program run slower because of extra function calls.
DThey are only useful for very simple programs and not recommended for complex automation.
Attempts:
2 left
💡 Hint
Think about how reusing code helps when you need to change something later.

Practice

(1/5)
1. What is the main benefit of using FUNCTION_BLOCK in PLC programming?
easy
A. It creates a graphical interface for the PLC.
B. It makes the program run faster by using less memory.
C. It automatically fixes errors in the code.
D. It allows you to reuse code and simplify program management.

Solution

  1. 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.
  2. Step 2: Identify the main benefit

    Reusing code saves time and makes the program easier to manage and maintain.
  3. Final Answer:

    It allows you to reuse code and simplify program management. -> Option D
  4. Quick Check:

    Reusable code = Simplify management [OK]
Hint: FUNCTION_BLOCK means reusable code blocks [OK]
Common Mistakes:
  • Thinking FUNCTION_BLOCK speeds up execution
  • Believing it fixes errors automatically
  • Confusing it with user interface creation
2. Which of the following is the correct way to declare a FUNCTION_BLOCK named Counter in PLC programming?
easy
A. FUNCTION Counter VAR_INPUT Count: INT; END_VAR END_FUNCTION
B. FUNCTION_BLOCK Counter VAR_INPUT Count: INT; END_VAR END_FUNCTION_BLOCK
C. FUNCTION_BLOCK Counter() VAR_INPUT Count: INT; END_VAR END_FUNCTION_BLOCK
D. FUNCTION_BLOCK Counter VAR_OUTPUT Count: INT; END_VAR END_FUNCTION_BLOCK

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    FUNCTION_BLOCK Counter VAR_INPUT Count: INT; END_VAR END_FUNCTION_BLOCK -> Option B
  4. Quick Check:

    Correct FUNCTION_BLOCK syntax = FUNCTION_BLOCK Counter VAR_INPUT Count: INT; END_VAR END_FUNCTION_BLOCK [OK]
Hint: FUNCTION_BLOCK uses VAR_INPUT for inputs, no parentheses [OK]
Common Mistakes:
  • Using FUNCTION instead of FUNCTION_BLOCK
  • Adding parentheses after FUNCTION_BLOCK name
  • Declaring inputs under VAR_OUTPUT
3. Given this FUNCTION_BLOCK code snippet, what will be the output value of 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?
medium
A. 8
B. 35
C. 0
D. Error: Result not assigned

Solution

  1. Step 1: Understand the FUNCTION_BLOCK logic

    The block adds inputs A and B and stores the sum in Result.
  2. Step 2: Substitute input values and calculate

    A = 3, B = 5, so Result = 3 + 5 = 8.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    3 + 5 = 8 [OK]
Hint: Add inputs to get output Result [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Assuming Result is zero by default
  • Thinking Result is not assigned automatically
4. Identify the error in this FUNCTION_BLOCK code snippet:
FUNCTION_BLOCK Multiply
VAR_INPUT
X: INT;
Y: INT;
END_VAR
VAR_OUTPUT
Product: INT;
END_VAR
Product = X * Y;
END_FUNCTION_BLOCK
medium
A. Incorrect use of '=' instead of ':=' for assignment.
B. VAR_OUTPUT should be declared before VAR_INPUT.
C. Missing semicolon at the end of the assignment line.
D. FUNCTION_BLOCK name must be lowercase.

Solution

  1. Step 1: Check assignment syntax in FUNCTION_BLOCK

    In PLC programming, assignment uses ':=' not '='.
  2. 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.
  3. Final Answer:

    Incorrect use of '=' instead of ':=' for assignment. -> Option A
  4. Quick Check:

    Use ':=' for assignment [OK]
Hint: Use ':=' for assignments in PLC code [OK]
Common Mistakes:
  • Using '=' instead of ':='
  • Thinking semicolon is optional
  • Believing VAR_OUTPUT must come first
5. You want to create a reusable FUNCTION_BLOCK that counts how many times an input boolean Trigger changes from FALSE to TRUE. Which approach correctly implements this behavior?
hard
A. Reset the counter to zero every time Trigger is TRUE.
B. Increment the counter every time the FUNCTION_BLOCK runs regardless of Trigger state.
C. Use a VAR to store previous Trigger state and increment a counter only when Trigger changes from FALSE to TRUE.
D. Use VAR_INPUT to store the counter and increment it inside the block.

Solution

  1. Step 1: Understand the counting condition

    The counter should increase only when Trigger changes from FALSE to TRUE (rising edge).
  2. 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.
  3. Final Answer:

    Use a VAR to store previous Trigger state and increment a counter only when Trigger changes from FALSE to TRUE. -> Option C
  4. Quick Check:

    Edge detection needs previous state storage [OK]
Hint: Store previous input to detect rising edge [OK]
Common Mistakes:
  • Incrementing counter every cycle
  • Resetting counter incorrectly
  • Using VAR_INPUT for 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