Q(uick)BASIC Statement: CALL
Quick View
CALL Statement
A statement that transfers control to another procedure, a BASIC SUB
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- [CALL] name [([argumentlist])]
Description/Parameter(s)
name | The name of the SUB procedure to call. |
argumentlist | The variables or constants to pass to the SUB procedure. Separate multiple arguments with commas. Specify array arguments with the array name followed by empty parentheses. |
If you omit the CALL keyword, also omit the parentheses around argumentlist. Either declare the procedure in a DECLARE statement before calling it, or save the program and QBasic automatically generates a DECLARE statement.
To specify an argument whose value will not be changed by the procedure, enclose the argument in parentheses.
Example
The program REMLINE.BAS illustrates calling SUB procedures. To view or run this program, load REMLINE.BAS using the Open command from the File menu.
See also:
Syntax
- CALL name[(argumentlist)]
Description/Parameter(s)
name | The name, limited to 40 characters, of the BASIC SUB being called. The name must appear in a SUB statement if the SUB is in the same module. |
argumentlist | The variables or constants passed to the subprogram. Arguments in the list are separated by commas. Arguments passed by reference can be changed by the subprogram. |
If the argumentlist includes an array argument, the array is specified by the array name followed by empty parentheses:
- DIM IntArray(1 TO 20)
- ⋮
- CALL ShellSort(IntArray())
When you use the CALL statement, the CALL keyword is optional. However, if you omit the CALL keyword, you must declare the procedure in a DECLARE statement. Notice also that when you omit the CALL keyword, you also omit the parentheses around the argument list.
Arguments are passed by reference: the subprogram is given the address of the argument. This allows subprograms to change the argument values. BASIC can also pass arguments by value. The following statement calls a subprogram and passes a single argument by value:
- CALL SolvePuzzle((StartValue))
Because StartValue is in parentheses, BASIC evaluates it as an expression. The result is stored in a temporary location, and the address of the temporary location is passed to the SUB. Any change made by the subprogram SolvePuzzle is made only to the temporary location and not to the variable.
Other Uses | |
To transfer control to a procedure written in another language: | ⮜ CALLS ⮞ |
To transfer control to a machine-language procedure: | ⮜ CALL ABSOLUTE ⮞ |
To perform a DOS system call: | ⮜ CALL INTERRUPT ⮞ |
Example
CALL_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the CALL statement. To look at the program in the View window and, optionally, to run it, load CALL_EX.BAS using the File menu's Open Program command.The program copies a series of files into a new file, the last file in the series entered from the command line. In the program, the BASIC subprogram PRINTOUT is called after first splitting the command line into separate file names and storing them in the array FILE$. The PRINTOUT subprogram copies the contents of the files to the final file in the list and to the standard output device (default is to your screen).
See also:
Syntax
- CALL name [([BYVAL] argument, [BYVAL] argument ...)]
- name [([BYVAL] argument, [BYVAL] argument ...)]
Description/Parameter(s)
This CALL statement transfers control to a BASIC SUB procedure. A different ⮜ CALL ⮞ statement is used to transfer control to a procedure written in another language.
- The argument name is limited to 40 characters.
- BYVAL specifies that the argument is passed by value, rather than by reference. This keyword is informational in the CALL statement. For more information, see Usage Notes.
- If you omit the optional CALL keyword, you must declare the procedure in a DECLARE statement and omit the parentheses around the list of arguments.
Usage Notes
- If the argument list includes an array argument, the array is specified by the array name followed by empty parentheses:
- DIM IntArray(1 TO 20)
- ⋮
- CALL ShellSort(IntArray())
- The CALL statement passes arguments by reference or by value, depending on how the argument is defined in the DECLARE or SUB statements. Passing an argument by reference means that the procedure is given the address of the argument. This allows procedures to change the argument values. Passing an argument by value means that the procedure is given the value of the argument. This allows the argument to change locally in the procedure without affecting the value of the variable in the rest of the program.
- Within the CALL statement, the BYVAL keyword is informational. An argument must be defined as being passed by value in the procedure or in the DECLARE statement for the procedure in order to pass that argument by value. For example:
- DECLARE SUB Mult (BYVAL X!, Y!)
- ⋮
- CALL Mult(BYVAL X!, Y!)
In this example, the BYVAL in the CALL statement is optional. Deleting it has no effect. Adding BYVAL to Y! in the CALL statement without changing the DECLARE statement would generate an error.
- If you omit the optional CALL keyword, you cannot follow the procedure name with a colon (:), the BASIC statement separation character. If you do, BASIC will treat the procedure name as a label instead of as a call to the procedure.
Example
This example uses the CALL statement to call a SUB procedure that prints a message on the 25th line of the display.
CLS
message$ = "The quick brown fox jumped over the lazy yellow dog."
CALL PrintMessage(message$)
END
SUB PrintMessage (message$)
LOCATE 24, 1
PRINT message$;
END SUB