Call Instruction  

This topic discusses the following subjects:

Using Subroutines

How to Create a Subroutine

How to Terminate a Subroutine

How to Call a Subroutine

Nesting and Recursion

Using Subroutines

(Back to Top )

Subroutines help you to partition your program. The instructions used in your main program determine the execution of the specific subroutine. When the main program calls the subroutine for execution, the subroutine carries out its program to its end. Then, the system returns control to the main program at the network from which the subroutine was called.

Subroutines are used to segment or divide your program into smaller more manageable blocks. You can take advantage of this benefit when debugging and performing maintenance on your program. By working with smaller blocks, you can easily debug and troubleshoot these areas as well as the entire program. The PLC can also be more efficiently used by only calling to the block when needed, as all blocks may not have to execute every scan.

Finally, subroutines can be portable if your subroutine references only its parameters and local memory. Make the subroutine avoiding any use of global variables/symbols (absolute addresses in I, Q, M, SM, AI, AQ, V, T, C, S, AC memory). If your subroutine has no calling parameters (IN, OUT, or IN_OUT), or uses only local variables in L memory, you can export the subroutine and import it into another project.

To use a subroutine in your program, you must perform three tasks:

·Create the subroutine

·Define its parameters (if any) in the Local Variable Table of the subroutine

·Call the subroutine from the appropriate POU (for example, your OB1 block, or another subroutine)

For LAD and FBD

STEP 7-Micro/WIN 32 automatically generates a subroutine call box instruction after the subroutine is created and its call parameters are defined. The call instruction has the correct number and type of input/output parameters, based on your Local Variable Table declarations for that subroutine. After a subroutine is created, it appears in the instruction tree. To insert the subroutine call into another POU, drag the subroutine block icon from the instruction tree and drop into another POU.

How to Create a Subroutine

(Back to Top )

To create a subroutine, use one of the following methods:

·From the Edit menu, choose Insert>Subroutine

·From the Instruction Tree, right click on the Program Block icon and select Insert > Subroutine from the popup menu

·From the Program Editor window, right-click and select Insert > Subroutine from the popup menu

The Program Editor changes from the display of the previous POU to the new subroutine. A new tab appears at the bottom of the Program Editor, identifying the new subroutine.

At this point, you can work on the new subroutine, or leave it alone and return to the POU where you were working previously:

·If you want to assign the parameters of the subroutine now, you can define them using the Local Variable Table for that subroutine.
* Remember that there is a separate Local Variable Table for each POU in your program. Local variables for this subroutine must be defined in the Local Variable Table that appears when you have the tab of this subroutine selected. Make sure that you have selected the appropriate tab any time you edit the Local Variable Table.
** The maximum limit of input/ output parameters for each subroutine call is 16. If you attempt to download a program that exceeds this limit, an error is returned.

·If you want to write the logic for this subroutine while its tab is selected, simply begin working in the Program Editor window.

·If you want to work on a different POU, click on the tab for that POU in order to display it in the Program Editor window.

Do Not Use an RET Instruction to Terminate Your Subroutines

(Back to Top )

You cannot use the END instruction in a subroutine.

The unconditional POU termination instructions (END for OB1, RET for SBR, and RETI for INT) are automatically inserted by the editors. An example is shown below.

How to Call a Subroutine

(Back to Top )

After you insert a new subroutine and define its parameters (if any) in the Local Variable Table of that subroutine, you can place a call to the subroutine in another POU of your program. (You can call a subroutine from OB1, another subroutine, or an interrupt routine; you cannot call the subroutine from itself.)


For LAD and FBD programs, a customized call box instruction is generated after you assign parameters to the subroutine in the Local Variable Table of the subroutine. This call instruction automatically includes the correct number and type of input and output parameters for the subroutine.

To insert the call instruction in a POU of your LAD or FBD program:

1.Open the desired POU in the Program Editor window and scroll to the network where you wish to insert the subroutine call.

2.In the Instruction Tree, double-click to open the Subroutines folder. You can either drag the appropriate call instruction from the tree and drop it on the appropriate cell of the network in the Program Editor, or place your cursor on the cell in the Program Editor and then double-click on the call instruction in the tree.

3.Edit the parameters of the call instruction in your program and assign valid operands to each parameters. Valid operands are as follows: memory addresses, constants, global symbols, local variables from the POU where the call instruction has been placed (not local variables from the subroutine that is being called).

Note: If you insert a call to a subroutine, and then later modify the Local Variable Table of that subroutine, the call becomes invalid. You must delete the invalid call and replace it with an up-to-date call instruction that reflects the correct parameters.


To insert a call in an STL program, use the Call Instruction.

When a subroutine is called, the entire logic stack is saved, the top of the stack is set to one, all other stack locations are set to zero, and control is transferred to the called subroutine. When this subroutine is completed, the stack is restored with the values saved at the point of call, and control is returned to the calling routine.

Accumulators are common to subroutines and the calling routine. No save or restore operation is performed on accumulators due to subroutine use.

Nesting and Recursion

(Back to Top )

You can have a total of 64 subroutines in a program (128 subroutines for CPU 226XM). You can nest subroutines (place a subroutine call within a subroutine), to a depth of eight. Direct recursion is allowed.