Passing Parameters

Authors

Publication

Pub Details

Date

Pages

See all articles from QL Hacker's Journal 24

When I was learning Pascal, one of the hurdles I had to get over was the concept of parameter passing. Parameters are used when passing data to procedures and functions. Parameters some in two type; “call by value” and “call by reference.” When you pass operation to a procedure, you are “calling” that procedure.

When using procedures and functions, you need to know of you are calling by reference or value. Each has different effects on your program and not knowing the effects can cause what is called “side effects.”

Call by Value – This means that the value of the parameter is passed to the procedure. The procedure only gets a copy of the data and what ever it does to the data, it does not affect the original variable. C defaults to call by value. SuperBasic defaults to call by value only when you use numbers.

Call by Reference – This means that the variable itself (or a reference to it) is passed to the procedure. If the procedure changes the value of the variable, it is changed through out the whole program. C uses pointers to get procedures to do call by reference. Superbasic defaults to call by reference when using variables.

When programing in C, you know when you are calling by value or reference. It is an important part of the language and taught early when learning C. In SuperBasic it is not very apparent and not really discussed. In fact, I’ve been programming in SuperBasic for over 10 years now and I did not know if it used call by reference or value. So I decided to play with SuperBasic, read the manual, and figure this out.

Below is a simple procedure that takes a variable as a parameter and increases it by one. We’ll call the procedure inc for increment.

10 DEFine PROCedure inc ( x )
20 LET x = x + 1
30 PRINT x
40 END DEFine

When you call the procedure with a literal:

100 inc 20    or    100  inc (20)

SuperBasic can only use call by value, since there is no variable to use in a call by reference.

When you call it with a variable:

      100 inc var

SuperBasic uses call by reference. This means that when you call the procedure, the value of VAR will increase by 1. In a way this procedure is working like a function called like this:

      100 var = inc (var)

Now the procedure inc will get its own copy of VAR to play with and the real variable VAR will not change.

The same works with functions. Since functions need parentheses around variables used in a call, a single set of parentheses is used for a call by Reference and a double set of parentheses is used for a call by Value.

Products

 

Downloadable Media

 

Image Gallery

Scroll to Top