Search code examples
twincat

How to make inputs of a function block method optional?


When calling a method of a function block, is it possible to make certain input variables optional? If I call fbA.methA() without assignments for all input variables, TwinCAT throws an error: "Function methA requires exactly 'x' inputs." There are times when some inputs are unnecessary or irrelevant, but so far I've had to assign dummy values to those inputs to get the code to compile.


Solution

  • I don't think that that is possible. You could make extra methods which all call a base method.

    For example:

    FUNCTION_BLOCK Multiplier
    
    METHOD Multiply : REAL
    VAR_INPUT
        number1 : REAL;
        number2 : REAL;
    END_VAR
    
    METHOD MultiplyByTwo : REAL
    VAR_INPUT
        number : REAL;
    END_VAR
    
    MultiplyByTwo := Multiply(2, number);
    

    That way you also reduce the number of inputs of your method, thereby making it easier to test and use.