Method
A method can be defined for either a Program or Function Block. A method is 'attached' to the scope of an object. Methods can be used to :
- Modularize re-usable pieces of code
- Allow outside access to modify the state/behavior of an instance
Syntax
Method Declaration
METHOD <MethodName> : <ReturnType?>
// Variable declarations
VAR
i : INT;
END_VAR
// Body
END_METHOD
A counter method
FUNCTION_BLOCK FB_Counter
VAR_OUTPUT
Count : INT;
END_VAR
METHOD M_CountUp
Count := Count + 1;
END_METHOD
END_FUNCTION_BLOCK
VAR
instA : FB_Counter;
END_VAR
instA.M_CountUp();
instA.M_CountUp();
print(instA.Count); // 2