IMDHMIControls LibraryShop main store

Troubleshooting

Why CODESYS 2.3 reports error 4051 is no function

PLC programming Published 5 min read By IMDHMI controls team

Short answer

CODESYS 2.3 raises error 4051 because the name in your call is not a FUNCTION. Throttle_Timer is declared as a FUNCTION_BLOCK, and IEC 61131-3 requires an instance variable before such a block can run: declare myTimer of type Throttle_Timer in PLC_PRG and call that instance with the delay preset of 500 ms. The instance call clears the compile error, but the WHILE loop still executes in one task cycle, so the timer never elapses; move the call into cyclic logic and increment only when the block reports completion.

On this page
  1. What error 4051 says and why the compiler raises it
  2. Declare an instance, then call the instance
  3. Why the WHILE loop still cannot pace the ramp
  4. Troubleshooting checklist
  5. The decision
  6. Questions asked

CODESYS 2.3 reports 4051 because the name you called is not a function at all. In the reported project, Throttle_Timer is declared as a FUNCTION_BLOCK, and IEC 61131-3 requires an instance variable before a function block can be invoked. The fix is two lines of declaration: create an instance such as myTimer of type Throttle_Timer in PLC_PRG and call that instance with the delay preset of 500 ms instead of calling the type name. That clears the compile error. It does not fix the behavior: the WHILE loop runs inside a single task cycle, so the timer inside the block never elapses and the throttle ramps to its limit in one scan. Call the instance once per task cycle and step the throttle only when the block reports that its timer has finished.

What error 4051 says and why the compiler raises it #

The compiler message reads 4051 '<name>' is no function: the placeholder is the exact name in your call. Beckhoff's error list for its CODESYS 2.3-based PLC engineering documents 4051 with exactly that text, and the remedy it gives is to use a name that resolves to a function defined in the project or in the libraries. The compiler found your name, looked it up, and discovered it names something that cannot be called like a function.

In this project the name is the POU type. The two POU kinds differ in exactly the way that matters here:

POU kind State between calls How it is called
FUNCTION None; it evaluates inputs and returns a value By its own name
FUNCTION_BLOCK Kept in an instance; here a TON named Delay must remember elapsed time Through a declared instance variable

Your block keeps a TON named Delay, whose whole job is to hold elapsed time from one scan to the next. That makes it a FUNCTION_BLOCK by design. IEC 61131-3 treats the block type and each instance as separate objects and requires an instance declaration before any call, so Throttle_Timer(T#500ms) asks the compiler to call a type, no function with that name exists, and 4051 is the correct answer. The confusion is common because in general-purpose languages the routine you declare and the thing you call share one name; in this standard they do not.

Declare an instance, then call the instance #

The block itself can stay as written, with its TIME input named delayTimeMs and its internal TON:

FUNCTION_BLOCK Throttle_Timer
VAR_INPUT
    delayTimeMs : TIME;
END_VAR
VAR_OUTPUT
    done : BOOL;        // added, see next section
END_VAR
VAR
    Delay : TON;
END_VAR

In the calling program, declare the instance and call the instance:

PROGRAM PLC_PRG
VAR
    myTimer : Throttle_Timer;   // the instance that owns the state
END_VAR
myTimer(delayTimeMs := T#500ms);

The declaration creates the stateful object; the call executes that one object and hands it the preset. Passing the parameter by name with the assignment operator is optional once the instance exists, and a bare positional call compiles too, but the named form keeps the call readable when the block grows more inputs.

The test that proves the result is a recompile: after the instance declaration and the instance call replace the type-name call, the project compiles without 4051. If you still see 4051, the name in the call is still a type or library object, not the instance variable you declared.

Why the WHILE loop still cannot pace the ramp #

Fixing the call makes the project compile, but the loop still misbehaves. Structured text executes a WHILE loop to completion within one task cycle, and a TON measures elapsed time across task cycles. Called repeatedly inside the loop, the internal TON sees the same instant on every pass, so its completion output never goes true, the RETURN never fires for the intended reason, and all the increments happen in one scan. Pulsing the timer input true and then false inside the same scan does not help either; the false edge simply resets what little elapsed time the timer accumulated. What a TON keeps doing after its input drops, and why the completion output can linger, is covered in why a CODESYS TON output stays on after its rung goes false. How the same timer behaves differently between structured text and ladder is covered in when a CODESYS TON works in structured text but not ladder.

Give the block a visible completion output and drive the call from cyclic logic:

Delay(IN := TRUE, PT := delayTimeMs);
done := Delay.Q;
PROGRAM PLC_PRG
VAR
    myTimer : Throttle_Timer;
END_VAR
myTimer(delayTimeMs := T#500ms);
IF speedUp AND myTimer.done AND Throttle < 99.51 THEN
    Throttle := Throttle + 0.5;
END_IF

Now the arithmetic works out. Steps of 0.5 up to the 99.51 limit give 99.5 divided by 0.5, which is 199 increments. Spaced at the preset, the ramp takes 199 multiplied by 0.5 seconds, about 99.5 seconds of running time with the button held. Each step completes within one task period of the preset, so on a 10 ms task each increment lands within roughly 510 ms of the previous one. Without the completion gate, all 199 increments land in one scan instead.

Troubleshooting checklist #

Work through these in order when the error or the pacing is wrong:

  • Read the name inside the 4051 message and find its declaration: FUNCTION compiles by name, FUNCTION_BLOCK does not.
  • Confirm the block holds state, such as a TON, which is the practical sign that it must be instantiated.
  • Declare one instance variable of the block type in the VAR section of the calling POU.
  • Replace every type-name call with a call to that instance, passing the TIME preset through the named input.
  • Give the block a BOOL output driven by the internal timer's completion output, so the caller can test it.
  • Move the call and the increment out of the WHILE loop and into the cyclic body of the program.
  • Recompile to confirm 4051 is gone, then run and confirm the value moves one step per preset interval, not all at once.

The decision #

A name declared as a FUNCTION_BLOCK can never be called by its type name in CODESYS 2.3; declare an instance and call the instance, and error 4051 is gone. The WHILE loop form is wrong regardless of the instance fix: a TON cannot elapse inside a single scan, so the increment belongs in cyclic logic, gated on the block's completion output. With those two changes, the ramp advances one 0.5 step per preset interval and takes about 99.5 seconds end to end instead of finishing in one scan.

Questions asked

What does the CODESYS 2.3 message 4051 is no function mean?

The compiler resolved the name in your call but found no FUNCTION with that name. In the reported project the name Throttle_Timer is the type of a FUNCTION_BLOCK, and a type cannot be called directly. Declare a variable of that type in the calling POU and invoke the variable instead; the error disappears at the next compile.

How is a FUNCTION_BLOCK different from a FUNCTION in IEC 61131-3?

A FUNCTION has no memory between calls: it takes inputs, computes a return value and forgets everything. A FUNCTION_BLOCK keeps internal state, in this case a TON named Delay whose elapsed time must survive from one scan to the next, so the standard requires each call to go through a declared instance variable that owns that state.

How long does the full throttle ramp take once the call is fixed?

Steps of 0.5 up to the 99.51 limit give 199 increments. At a preset of 500 ms per increment the ramp takes 99.5 seconds of running time. Each step also lands within one task period of the preset, so a 10 ms task finishes each step within about 510 ms.

Keep reading

  • Troubleshooting

    Why CODESYS rejects an array of derived function blocks

    CODESYS and TwinCAT 3 refuse ARRAY OF BasicValve on a parameter typed ARRAY[*] OF AValve because the array itself is a value type: its storage is element size times element count, and inheritance is checked per object, never between array types. Convert each element into an array of the base interface, or build an array of POINTER TO the base function block, and pass that instead.

    PLC programming

  • Troubleshooting

    CX-Programmer invalid argument in function blocks on Windows 11

    CX-Programmer throws "An invalid argument was encountered" on Windows 11 when a function block contains a signed or comma-decimal constant, because the block compiler parses such literals through the Windows regional decimal symbol. Rewrite the literal as an unsigned decimal constant or as a REAL variable with an initial value, set a period-decimal regional format, install the current CX-One update, and recompile the block offline before any transfer.

    PLC programming

  • Troubleshooting

    Why GX Works 2 ST pulse timer TP fails with error C9009

    Error C9009 on a structured text TP pulse timer in GX Works 2 is a compile-stage failure in device/label automatic assignment, not a broken timer. Test the unchanged FX3S project in GX Works 2 1.620W first: the same code that fails in 1.586L compiles there, so the installed build is the first suspect. Widen the automatic-assign timer range only when counted label consumption proves a real shortage.

    PLC programming