Creating a Function Without Input Parameters (ST)

In Mervis IDE, it is fully supported to define a function in Structured Text (ST) that does not require any input parameters (i.e., it has no VAR_INPUT section).

This is useful for functions that return constant values, system states, or perform actions based on global variables/internal logic rather than arguments.

If you attempt to skip the VAR_INPUT section, you must ensure your VAR_OUTPUT or local variables immediately follow the function declaration.

FUNCTION MyFunction : REAL
    VAR_OUTPUT
        outVal : REAL;
    END_VAR
 
    // Logic
    outVal := 5.0;
 
END_FUNCTION

Users often encounter the error: In function declaration no viable alternative at input “VAR_OUTPUT”.

This usually happens if the structure of the variable declaration block is malformed. Ensure that you declare the function return type (e.g., : REAL) correctly before starting the VAR_OUTPUT block.

Declaration:

FUNCTION GetPi : REAL
    VAR_OUTPUT
        val : REAL;
    END_VAR
 
    val := 3.14159;
END_FUNCTION

Calling the function: Since there are no inputs, you can call the function with empty parentheses or simply by name, depending on the context:

PROGRAM Main
    VAR
        piValue : REAL;
    END_VAR
 
    piValue := GetPi(); 
END_PROGRAM
  • © Energocentrum Plus, s.r.o. 2017 - 2026