example - SwapNums function in ST (TC3)

#PLC #TwinCAT3 #Beckhoff

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

This function takes 2 numbers and swaps its value using a temporal variable.

Copy the PLC Code:

FUNCTION SwapNums
VAR_IN_OUT
	nNumber1 : INT;
	nNumber2 : INT;
END_VAR
VAR
	nNumberTemp : INT;
END_VAR
----------------------------------------------------------------------
nNumberTemp := nNumber1;
nNumber1 := nNumber2;
nNumber2 := nNumberTemp;
PROGRAM MAIN
VAR
	nA : INT := 5;
	nB : INT := 15;
END_VAR

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

SwapNums(nNumber1 := nA,
		 nNumber2 := nB);

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.