Introduction
A function block groups code and data together to perform a specific task repeatedly in a PLC program. It helps organize and reuse logic easily.
FUNCTION_BLOCK <Name> VAR_INPUT <input_variable>: <type>; END_VAR VAR_OUTPUT <output_variable>: <type>; END_VAR VAR <internal_variable>: <type>; END_VAR (* Your code here *) END_FUNCTION_BLOCK
FUNCTION_BLOCK MotorControl VAR_INPUT Start: BOOL; Stop: BOOL; END_VAR VAR_OUTPUT Running: BOOL; END_VAR VAR State: BOOL; END_VAR (* Simple start/stop logic *) IF Start THEN State := TRUE; ELSIF Stop THEN State := FALSE; END_IF Running := State; END_FUNCTION_BLOCK
FUNCTION_BLOCK Timer VAR_INPUT Enable: BOOL; TimeSet: TIME; END_VAR VAR_OUTPUT Done: BOOL; END_VAR VAR Elapsed: TIME; END_VAR (* Timer logic here *) IF Enable THEN Elapsed := Elapsed + T#100ms; IF Elapsed >= TimeSet THEN Done := TRUE; ELSE Done := FALSE; END_IF ELSE Elapsed := T#0s; Done := FALSE; END_IF END_FUNCTION_BLOCK
FUNCTION_BLOCK SimpleCounter VAR_INPUT Increment: BOOL; END_VAR VAR_OUTPUT Count: INT; END_VAR VAR InternalCount: INT := 0; END_VAR IF Increment THEN InternalCount := InternalCount + 1; END_IF Count := InternalCount; END_FUNCTION_BLOCK (* Main program *) PROGRAM Main VAR Counter1: SimpleCounter; IncSignal: BOOL := TRUE; END_VAR (* Simulate calling the function block 3 times with Increment TRUE *) Counter1(Increment := IncSignal); Counter1(Increment := IncSignal); Counter1(Increment := IncSignal); (* Output the count *) END_PROGRAM
Function Block in PLC programming?MotorControl in IEC 61131-3 structured text?FUNCTION_BLOCK followed by the name.FUNCTION_BLOCK MotorControl, which is correct. Options A, B, and D use other keywords for different constructs.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();
Run checks if In is TRUE, then increments Counter by 1.In = TRUECounter from 0 to 1, second call increments from 1 to 2. Output is set to Counter.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
Count := Count + 1 is missing a semicolon at the end, which is required in structured text.METHOD keyword is valid, output variable can be named Status, and underscores are allowed in names.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?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.VAR variables inside function blocks keep their value between calls, acting like memory.Pass the test and this pattern moves unknown