example - GreaterSmallerThan function in ST (TC3)

#PLC #TwinCAT3 #Beckhoff

Learn more at Part 5 - Structures & functions (IEC 61131-3)

This function takes two numbers and returns which one is the Greater and which one is Smaller.

Note the way to assign output values from a function to variables. We use =>, which is the assignment operator for outputs. You can learn more here

Copy the PLC Code:

FUNCTION Greater_Smaller
VAR_INPUT
	nNumber1 : INT;
	nNumber2 : INT;
END_VAR
VAR_OUTPUT
	nGreater : INT;
	nSmaller : INT;
END_VAR
----------------------------------------------------------------------
IF nNumber1 > nNumber2 THEN
	nGreater := nNumber1;
	nSmaller := nNumber2;
ELSE
	nGreater := nNumber2;
	nSmaller := nNumber1;
END_IF
PROGRAM MAIN
VAR
	nReturnGreater : INT;
	nReturnSmaller : INT;
END_VAR

-----------------------------------------------------------------------

Greater_Smaller(nNumber1 := 8,
				nNumber2 := 15,
				nGreater => nReturnGreater,
				nSmaller => nReturnSmaller);

How to create a Function


To create a function, right-click on POUs, select Add, Select POU.
400

Write the name of the function and fill in the data type of the return value. Make sure that structured text is selected.

This will create a shell for your function and now you can fill in the implementation.