User`s guide
E-Prime User’s Guide
Chapter 4: Using E-Basic
Page 156
'Subroutine containing the script necessary to set the
'background to red.
Dim cnvs As Canvas
Sub ClearToRed
cnvs.FillColor = CColor("Red")
cnvs.Clear
End Sub
In the example below, the ClearToRed subroutine is called to quickly set the background to red
before 10 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
'size.
Dim i As Integer
Dim x As Integer
Dim y As Integer
Dim rad As Integer
Set cnvs = Display.Canvas
ClearToRed
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
Next I
Sleep 1000
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.
4.6.3 Writing Functions
Like subroutines, functions are units composed of a series of script commands. Functions differ
from subroutines in that they may be used in a command, and may return a value (e.g., if the
function requested a response from the user, or performed a data transformation).
4.6.3.1 Example: Calculate a mean value
In the example below, the DoMean function is passed two parameters (total and count). Total is
divided by count (using the "/" operator) to determine the value for DoMean. This script is
entered on the User tab of the Script window to define the DoMean function.
Function DoMean(total As Double, count As Integer)As Double
DoMean = total/count
End Function