omron — Subroutines

Subroutines Subroutines There are three methods of exchanging information between programs: l global variables l soft-signals l program argument list When using global variables, simply use the same variable names in the different programs. Unless used carefully, this method can make program execution unpredictable and hard to debug. It also makes it difficult to write generalized subroutines because the variable names in the main program and subroutine must always be the same. Soft-signals are internal program signals. These are digital software switches whose state can be read and set by all tasks and programs (including across CPUs in multiple CPU systems). See "Soft Signals" for details.

Exchanging information through the program argument list gives you better control over changes made to variables. It also eliminates the requirement that the variable names in the calling program be the same as the names in the subroutine. The following sections describe exchanging data through the program parameter list. Argument Passing There are two important considerations when passing an argument list from a calling program to a subroutine. The first is making sure the calling program passes arguments in the way the subroutine expects to receive them (mapping). The second is determining how you want the subroutine to be able to alter the variables (passing by value or reference).

Mapping the Argument List An argument list is a list of variables or values separated by commas. The argument list passed to a calling program must match the subroutine's argument list in number of arguments and data type of each argument (see Undefined Arguments on page 31). The variable names do not have to match. When a calling program passes an argument list to a subroutine, the subroutine does not look at the variable names in the list but the position of the arguments in the list. The argument list in the CALL statement is mapped item for item to the argument list of the subroutine.

It is this mapping feature that allows you to write generalized subroutines that can be called by any number of different programs, regardless of the actual values or variable names the calling program uses. The following figure shows the mapping of an argument list in a CALL statement to the argument list in a subroutine. The arrows indicate that each item in the list must match in position and data type but not necessarily in name. (The CALL statement argument list can include values and expressions as well as variable names.) eV+Language User's Guide, v2.x, 18318-000 Rev A Page 28