From GAB Help
Jump to: navigation, search

A subroutine is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code. Subroutines are callable routines used out of events on screen controls, or explicitly called to execute code that has been written. A subroutine will not run unless it is explicitly called.


Subroutines can perform calculations, lookup information, create data for a control on a screen, save data that has been changed, or fill in information from prior saves, there are many more things the program can use subroutines for, but these are a few to give you some ideas.

Advantages to breaking a program up into subroutines, include:

  • Reducing the duplication of code in a program (e.g., by replicating useful functionality, such as mathematical functions)
  • enabling reuse of code across multiple programs
  • deconstructing complex problems into simpler pieces (this improves maintainability and ease of extension)
  • improving readability of a program
  • hiding or regulating part of the program (see Information hiding)


'Body of the code here

Components of a Subroutine

  • The body of code to be executed when the subroutine is called. This code is a fragment executed on the call of the subroutine.
  • Parameters (or Passed Arguments) are elements passed to the subroutine from the point where it is called.
  • Subroutines may be passed variable information; these variables are called passed arguments. (optional)
  • A return value (or return values) may be passed back to the calling subroutine (optional).

General Subroutines

A general subroutine tells the application how to perform a specific task. Once a general subroutine is defined, it must be specifically invoked by the application. By contrast, an event subroutine remains idle until called upon to respond to events caused by the user or triggered by the system.

Why create general subroutines?

One reason is that several different event subroutines might need the same actions performed. A good programming strategy is to put common statements in a separate subroutine (a general subroutine) and have your event subroutines call it. This eliminates the need to duplicate code and also makes the application easier to maintain.

Passed/Return Variables for Subroutines

Pass Variables or Values to a Subroutine

Function.Intrinsic.Control.CallSub(SubName, Arg Name1, Arg Value1, Arg Name2, Arg Value2, etc.)

Return Variables or Values from a Subroutine

Fuction.Intrinsic.Variable.AddRV (Name, Value)


Function.Intrinsic.Variable.AddRV (“Return Variable”, Variable.Local.retVar)

Event Subroutines

When an event has occurred (such as a click on a button), it automatically invokes the event subroutine using the name corresponding to the event. The subroutine is linked to the event through the GAB Forms Designer.


An example would be mouse track on a form. The forms designer is used to select the subroutine on the form select the mousemove and mousedown events and then select the mousetrack subroutine.

Form Designer will write the following lines behind the scene:


Here is the code that you might put in the subroutine being called by the event:


Passed Argument Example

Variable.Local.a,"SecondNumber", v.Local.b)

Function.Intrinsic.Math.add(Variable.args.firstnumber, Variable.args.secondnumber, Variable.Local.result)
Function.Intrinsic.Variable.AddRV("A2Nresult", Variable.Local.result)

Ending a program

With Screens – the program waits until it is told to end the program, using this call to do that Program.Sub.Control.End

Without Screens – the program will end automatically after the last call

Call Stack

A call stack is the list of “Calls” of Subs for a specific program, they are listed in order of when they are called so that they execute in the order expected.

  • ScreenSU
  • Preflight
  • Include Files (if any)
  • Main
  • Subroutines

Include Files

Include files are “programs or modules” that can be reused in other programs. Will show up under preflight to be selected to add to current program. These include files can be referenced within a GTL program.