ReadyBASIC
BASIC Language Reference Manual > Command and Function Reference >

CALL Command

Summary

Calls (invokes) a procedure.

Syntax

  • CALL ProcedureName

  • CALL ProcedureName()

  • CALL ProcedureName(ParameterName1)

  • CALL ProcedureName(ParameterName1, ..., ParameterNameN)

Remarks and Examples

A procedure is a block of code that performs some task. Procedures are defined using the PROCEDURE statement.

Once a procedure has been defined, you can then call or invoke the procedure as many times as you like by using the CALL command. Each time you call a procedure, the program flow jumps to the the procedure definition and the commands in the procedure definition are executed. When the end of procedure is reached, the control passes back to the place where the procedure was called. Program execution resumes with the instruction immediately after the procedure call.

If the procedure takes no parameters, then you can use either the form CALL MyProcedure or the form CALL MyProcedure().

Example program listing illustrating a simple procedure:

Main:
    PRINT "Hello!"
    CALL CountToFive
    CALL CountToFive
    PRINT "One more time..."
    CALL CountToFive
    END
    
PROCEDURE CountToFive
    FOR X% = 1 TO 5
        PRINT X%; " ";
    NEXT X%
    PRINT
END PROCEDURE

When this program is run, the results are as follows:

Hello!
1 2 3 4 5
1 2 3 4 5
One more time....
1 2 3 4 5

If a procedure takes one or more parameters, then values matching the parameters must be supplied in a list, with values separated by commas, and surrounded by parentheses, as shown in the following example.

Main:
    CALL DisplayInfo("Bob", "Smith", 555222, 9305.677)
    END

PROCEDURE DisplayInfo (FirstName$, LastName$, AccountNumber%, AccountBalance)
    PRINT "Customer: "; FirstName$; " "; LastName$
    PRINT "Account number: "; AccountNumber%
    PRINT "Account balance: $";
    CALL PrintNumberWithTwoDecimalPlaces(AccountBalance)
END PROCEDURE

PROCEDURE PrintNumberWithTwoDecimalPlaces (Value)
    PRINT ROUND(Value * 100) / 100
END PROCEDURE

Output:

Customer: Bob Smith
Account number: 555222
Account balance: $9305.68

Returning values to the caller by means of parameters

Procedures in ReadyBASIC follow a principle called pass-by-reference parameter passing.

This means that the variables in the procedure definition are linked with any corresponding variables that were used in the call to the procedure. If a parameter variable's value is modified in a procedure, and if the procedure call used a variable to supply the value for that parameter, the new value will be reflected in the original variable after the procedure call.

As this description is very abstract, an example will illustrate this property more clearly:

Main:
    MyValue% = 5
    PRINT "Before the procedure call, MyValue% = "; MyValue%
    CALL ModifyMyValue(MyValue%)
    PRINT "After the procedure call, MyValue% = "; MyValue%
    END
    
 PROCEDURE ModifyMyValue(X%)
     PRINT "Initial value of X% is "; X%
     X% = 200 : REM Change the value
     PRINT "X% has been changed to be "; X%
 END PROCEDURE

Result:

Before the procedure call, MyValue% is 5
Initial value of X% is 5
X% has been changed to be 200
After the procedure call, MyValue% is 200

The call to ModifyMyValue has connected the MyValue% variable in the main program to the X% parameter variable in the ModifyMyValue procedure. When the procedure modifies X%, the change is reflected in the MyValue% variable in the main program.

This does not mean that all procedure calls must always use variables to supply parameter values. You can also use literal values, such as a number like 333 or a string like "Bob". A statement such as "CALL ModifyMyValue(333)" would thus perfectly legitimate in the above program. (But if your goal is to have the procedure communicate one or more values back to the caller, you'll need to use variables in order to receive those values.)

Please note that functions (see the FUNCTION command) do not use the pass-by-reference parameter passing principle. Functions use pass-by-value parameters, which means that changing the values of parameter variables within a function definition will not affect the values of variables used to supply parameters in a function call.

Notes

For more information and examples, please see the PROCEDURE command.


See also: FUNCTION, GOSUB, LOCAL, PROCEDURE, RETURN




Copyright 2006-2012, Kevin Matz, All Rights Reserved.