4

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.

kolyur
  • 457
  • 2
  • 13

2 Answers2

6

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.

Roald
  • 2,459
  • 16
  • 43
  • 3
    Roald is right. However, in the future we have some hope: https://stackoverflow.com/a/62768754/8140625 – Quirzo Apr 09 '21 at 07:12
0

You also could screen the parameters as they are passed in (still requires parameters but they have no meaning aka always pass "0").

FUNCTION_BLOCK CAT
    METHOD DECIBELS: REAL
        VAR_INPUT
            MEOW, PURR: BOOL;
        END_VAR
        // body
        DECIBELS := 0.0;
        IF MEOW <> 0
            DECIBELS := DECIBELS + 10.0;
        END_IF;
        IF PURR <> 0
            DECIBELS := DECIBELS + 5.0;
        END_IF;
    END_METHOD
END_FUNCTION_BLOCK

you can invoke this like:

PROGRAM MAIN
    VAR
        C: CAT;
        RESULT: ARRAY [1..4] OF REAL;
    END_VAR
    // body
    RESULT[1] := C.DECIBELS(TRUE, TRUE); // will return 15.0
    RESULT[2] := C.DECIBELS(TRUE, 0); // will return 10.0
    RESULT[3] := C.DECIBELS(0, TRUE); // will return 5.0
    RESULT[4] := C.DECIBELS(0, 0); // will return 0.0
END_PROGRAM

Hope this helps

WAS_DEF
  • 1
  • 1