Previous | Contents | Index |
The purpose of the optional parameters is to pass data in and out of routines.
The parameter NAMES are used to pass data into and out of a routine. You can have up to 16 INPUT (with) and 16 OUTPUT (returning) parameters (32 total).
Currently the RETURNING parameters must all be non-array elements.
ROUTINE routine_name: PRIVATE var, var, var, ... or ROUTINE routine_name: PRIVATE INTEGER var, STRING var, STRING var, ... |
Example 3-13 Private Variables in Routines |
---|
do_totals end routine do_totals: private mytotal, desc$ mytotal = 15 desc$ = 'Test Totals' print desc$; mytotal end routine Test Totals 15 |
Sheerpower allows you to use private variables in a routine. Private variables are variables identified with a specific routine. This option allows you to use the same variable names more than once because, internally, Sheerpower prefixes the variables with the routine name and a "$" character.
In the above example, the private variables "mytotal" and "desc$" are
internally known to Sheerpower as:
do_totals$mytotal
do_totals$desc$
From inside the routine, you can reference the variables by their private names. For example: mytotal, desc$
From outside the routine (or while debugging the code), you can reference the private variables by their internal known names. For example: do_totals$mytotal, do_totals$desc$
See Appendix M, Sheerpower and Program Segmentation for more on routines and private routines in Sheerpower. |
ROUTINE routine_name [WITH input_param value [, input_param value, ...]] [, RETURNING output_param varname [, output_param varname,...]] [, PRIVATE varname,...] |
Example 3-14 Passing Parameters with Private Variables |
---|
do_a_heading with option 45, title 'Big test', returning status s print 'Status was: '; s stop routine do_a_heading with title, option, returning status, private tlen tlen = len(title) print '** '; title; ' **... option:'; option; ', len: '; tlen status = -1 end routine ** Big test **... option: 45 , len: 8 Status was: -1 |
Example 3-15 Error Messages when Passing Routine Parameters |
---|
do_totals with tttitle "my title" stop routine do_totals with title print title end routine |
The above example would generate TWO errors:
Possible coding error - variables used, but not assigned: DO_TOTALS$TITLE (no data value got into the TITLE parameter) Inconsistant ROUTINE parameter passing: Routine DO_TOTALS, Parameter TTTITLE (This isn't the right spelling of the TITLE parameter) |
Each program unit is made up of program lines. A program line consists of one or more statements.
When Sheerpower executes a program, it starts at the first line and executes all the statements in that program line. Then it goes to the next line and executes all the statements in that line and so on.
Multiple statements can be placed on a single line, delimited by the \ (backslash). CHR$(10) also signals the same as a \. A \ terminating the line implies an "&".
Example 3-16 Multiple statements on a single line |
---|
for z1 = 4. to 7. step .5 \ print z1 \ next z1 end run 4 4.5 5 5.5 6 6.5 7 |
Putting multiple statements on a single line is not recommended. It makes changing code difficult and the logic flow is not easy to follow. |
3.6.1 Continuing Program Lines and Implied Continuation
You can continue a statement on the next physical line by using an ampersand (&). This allows you to continue a statement that is too long to fit on one line.
To continue a statement, place the ampersand at the end of the continued line. You can also place an optional ampersand at the beginning of the next line. For example:
Example 3-17 Continuing Program Lines With "&" (AMPERSAND) |
---|
input 'Please enter your name': name$ print 'Hello, '; name$ print 'Today is '; day$ print name$; & ', have a good '; & // required ampersand & day$ // optional ampersand end Please enter your name? Julian Hello, Julian Today is Tuesday Julian, have a good Tuesday |
In the above example, the comment text follows the line continuation character (&). Whenever you comment a continued line, the (//) double forward slash must come AFTER the ampersand. |
You can continue statements between words; however, you cannot break up a word.
An ampersand (&) may be used to identify a statement that is continued on the next physical line. The ampersand is acceptable,but is no longer required in some cases. Implied continuation has been implemented for:
Comma-separated list continuation does not work in a PRINT statement. PRINT statements still require the trailing "&" for continuation. |
Example 3-18 Comma-Separated List Continuation |
---|
open table cust: name 'sptools:customer', // no ampersand access outin, // no ampersand lock close table cust end |
Example 3-19 Implied Continuation With '+', 'AND' |
---|
print 'Today is the first day of the ' + // no ampersand 'rest of your life.' a$ = 'walk' b$ = 'walk' c$ = 'walk' if a$ = b$ and // no ampersand a$ = c$ then print 'All are the same.' end if end Today is the first day of the rest of your life. All are the same. |
Inserting COMMENTS in programs will allow the code to be easily understood in the future. Comments are not executable statements. They are simply included in source code for informational purposes. They are seen when a program is listed or printed out. However, Sheerpower will ignore them when it executes a program.
There are two types of comments allowed in Sheerpower: exclamation point (!) and double-forward slash (//). The exclamation point commenting is used for creating the program and routine headers. The double-forward slash commenting is used for inserting comments within routines.
To comment out an entire single line or block of code using a programming keystroke, highlight the lines to be commented and press the GOLD + / keystroke.
You can also place your cursor anywhere on a line and press GOLD + /. The line will be automatically commented out and the cursor will be placed at the beginning of the next line. Blank lines are ignored. See Appendix F, Keystrokes for Sheerpower Rapid Development Environment for a full list of special keystrokes (and creating custom keystrokes) in SPDEV.
Example 3-20 Comments In Programs With // |
---|
dim name$(10) // setup array rem main logic for i = 1 to 10 // Begin the loop input 'Please enter your name': name$(i) // ask for a name if _exit then exit for // end if they want print 'Hello, '; name$(i) // print hello next i // end the loop end Please enter your name? Mary Hello, Mary Please enter your name? exit <---- type in 'exit' |
Previous | Next | Contents | Index |