Subroutines are composed of a series of commands combined into a unit. This unit may then be run by a call to the subroutine from within an InLine object. A subroutine is defined using the Sub…End Sub statement. For example, a simple subroutine may be created to “clear” the background of a Canvas object to a particular color.
Example: Clear the Screen to the Current Color
In the script below, the ClearToRed subroutine sets the fill color to red, and then uses the Clear command to clear the screen using the current FillColor setting. the following script would need entered on the User script tab in the Script window. Once defined, the subroutine is available to any InLine object, and the ClearToRead subroutine can be run by referring to it by name.
|'Subroutine containing the script necessary to set the|
|'background to red.|
|Dim cnvs As Canvas|
|cnvs.FillColor = Ccolor("Red")|
In the example below, the ClearToRed subroutine is called to quickly set the background to red before ten circles are drawn on the Canvas. The script below would be placed in an InLine object called during the experiment.
|'Clear the screen to red and draw 10 circles of random|
|Dim i As Integer|
|Dim x As Integer|
|Dim y As Integer|
|Dim rad As Integer|
|Set cnvs = Display.Canvas|
|x = 50|
|y = 100|
|For i = 1 To 10|
|cnvs.Pencolor = Ccolor("white")|
|cnvs.Fillcolor = Ccolor("white")|
|rad = Random (3,20)|
|x = x + 50|
|cnvs.Circle x, y, rad|
Subroutines are most useful when a section of script is used repetitively. The use of subroutines aids in the prevention of errors, and minimizes script maintenance. Additionally, it should be noted that subroutines may only be used in the User Script. Using subroutines in an InLine Object will result in compile errors.