SPLat Logo

Subroutines: What are they?

NOTICE: We are updating our website. For product enquiries and orders please contact us directly.
NOTICE: SPLat Controls has moved. We are now at 1/85 Brunel Rd, Seaford, 3198. map

Subroutines: What are they?

A subroutine is a block of code that can be invoked ("called") from anywhere in a program, indeed from many places in a program, and when completed can return control back to the main program. Suppose you have a program where, in several places, you need to turn on a pump if analog input A is more than 39 and the digital input PumpEnable is ON. Otherwise you want the pump off. This code will do it, at least in one place within the program:

          GoIfInOff   PumpEnable,PumpOff
          AnInA
          GoIfXLE     39,PumpOff
          ON          Pump
          GoTo        Continue
PumpOff:  Off         Pump
Continue: ;end of code fragment ... carry on

Let's call this bit of functionality "PumpServe". Imagine we need to do this "thing" in 3 different places in a program. We could achieve that by copying and pasting the same code into 3 places, and incidentally changing the line labels Pumpoff and Continue each time. Alternatively we could make "PumpServe" into a subroutine.

Subroutine concepts1          

The above drawing illustrates how a subroutine is used. In the main program (left) there are several references (calls) to the subroutine, using GoSub (Go to Subroutine) instructions. Each time the subroutine is called, SPLatty jumps across to the single copy of the PumpServe code, but remembers where he came from. When he is has done the PumpServe operation he returns to the line following the GoSub instruction. This is done with a Return instruction. Hence, a GoSub is similar to a GoTo, but has the added feature of remembering where it came from, so SPLatty can find his way back again. There is a special memory within SPLat called a "subroutine stack", that remembers the return address.

A subroutine may call another subroutine, and that one may in turn call another subroutine. However, you cannot "nest" subroutines indefinitely. The limit is 4 nested subroutines in dialects prior to 12, and 6 in dialect 12 and later.

Let's now re-write the PumpServe function as a subroutine:

PumpServe:
          GoIfInOff   PumpEnable,PumpOff
          AnInA
          GoIfXLE     39,PumpOff
          ON          Pump
          Return
PumpOff:  Off         Pump
          Return

You will notice there is no longer any need to have the GoTo Continue, as it is perfectly OK to have a Return instruction in the middle of the subroutine.

Postscript:

Some programmers maintain that it is bad practice to write a subroutine with multiple return instructions, as I did above. If you are just starting out it may be a moot point. As you gain experience you may agree, on the basis that with only a single exit point from a subroutine there are fewer hazards if you alter the code for some reason. Re-writing the above example in line with this principle, we would get:

PumpServe:
          GoIfInOff   PumpEnable,PumpOff
          AnInA
          GoIfXLE     39,PumpOff
          ON          Pump
          GoTo        PumpRet
PumpOff:  Off         Pump
PumpRet:  Return