Subroutine

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.

Benefits

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)

Syntax

Program.Sub.SubName.Start
'Body of the code here
Program.Sub.SubName.End

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)

Example

Variable.local.RetVar.declare(number)
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.

Example

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:

Gui.Form1..event(MOUSEMOVE,mousetrack)
Gui.Form1..event(MOUSEDOWN,mousetrack)

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

program.sub.mousetrack.start
Gui.form1.txtUser.text(Variable.args.X)
Gui.form1.txtLastName.text(Variable.args.y)
Function.Intrinsic.control.if(Variable.args.button,=,1)
Gui.form1..circle(v.args.X,v.args.y,20,v.color.white)
Gui.form1..circle(v.args.X,v.args.y,40,v.color.white)
Function.Intrinsic.control.elseif(v.args.button,=,2)
Gui.form1..clearscreen
Function.Intrinsic.control.endif
program.sub.mousetrack.end

Passed Argument Example

program.sub.main.start
Variable.Local.a.Declare(float)
Variable.Local.b.Declare(float)
Variable.Local.c.Declare(float)
Variable.Local.a.set(2)
Variable.Local.b.set(9.5)
Function.Intrinsic.Control.callsub("Add2Numbers","FirstNumber",
Variable.Local.a,"SecondNumber", v.Local.b)
Variable.Local.c.set(Variable.args.A2Nresult)
Function.Intrinsic.ui.msgbox(Variable.Local.c)
program.sub.main.end

program.sub.Add2Numbers.start
Variable.Local.result.Declare(float)
Function.Intrinsic.Math.add(Variable.args.firstnumber, Variable.args.secondnumber, Variable.Local.result)
Function.Intrinsic.Variable.AddRV("A2Nresult", Variable.Local.result)
program.sub.Add2Numbers.end

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.