Friday, May 3, 2024

📚 FUNCTION (and SUB): variable arguments, by default, are "passed by reference"

  • Preamble
  • A primer on "call-by-reference" vs "call-by-value"
  • BAM HOWTO
(BTW:  "call-by-reference" aka "pass-by-reference", and "call-by-value" aka "pass-by-value".)

Preamble

I started working on BASIC Anywhere Machine back in December 2020 with the version of wwwBASIC I found back then.

Although I've been modifying the copy of wwwBASIC embedded in BAM right from the start, I've never noticed the following until just a few days ago:

In function and subroutine calls,  
a variable as an argument
is always "called by reference"
and never "called by value".

Being a career Gupta Team Developer (aka Gupta SQL windows) programmer, I am accustomed to variables always being "called by value" unless explicitly flagged as being "called by reference".  So I immediately thought that the wwwBASIC code was misbehaving, and I started digging into the code to "fix" things.

Well, come to find out: wwwBASIC's behaviour matches QB64's behaviour (the same for, I imagine, QBASIC), and I'm thinking wwwBASIC contributors did that by design.

Personally, I'd like for BAM to behave (regarding function/sub arguments) in the same way as Gupta Team Developer.  However, compatibility with QB64 / QBASIC is much to important to me (pretty much as important as GW-BASIC compatibility.

A primer on "pass-by-reference" vs "pass-by-value"

(From Wikipedia)

Call by reference (or pass by reference) is an evaluation strategy where a parameter is bound to an implicit reference to the variable used as argument, rather than a copy of its value. This typically means that the function can modify (i.e., assign to) the variable used as argument—something that will be seen by its caller.

In call by value (or pass by value), the evaluated value of the argument expression is bound to the corresponding variable in the function (frequently by copying the value into a new memory region). If the function or procedure is able to assign values to its parameters, only its local variable is assigned—that is, anything passed into a function call is unchanged in the caller's scope when the function returns.

 

BAM HOWTO

Call by reference

Copy, paste, and run the following program in BASIC Anywhere Machine:

DIM a$ = "Howdy"

FUNCTION hello$( p$ )
  p$ = p$ + " there"
  hello$ = p$
END FUNCTION

PRINT "a$: " + a$
PRINT "result of hello$( a$ ): " + hello$( a$ )
PRINT "a$: " + a$

When run, you'll find that the value of a$ has been altered by the function:






Call by value

In BAM, the same as in QB64 Phoenix Edition:  To pass parameter variables by value (in order to protect the value in the variable used in the call), wrap the parameter variable in parentheses to turn the parameter into an expression.

Copy, paste, and run the following program in BASIC Anywhere Machine:

DIM a$ = "Howdy"

FUNCTION hello$( p$ )
  p$ = p$ + " there"
  hello$ = p$
END FUNCTION

PRINT "a$: " + a$
PRINT "result of hello$( a$ ): " + hello$( ( a$ ) )
PRINT "a$: " + a$

When run, you'll find that the value of a$ has NOT been altered by the function:














No comments:

Post a Comment

🖥 Fan Dance

This is a port and mod of a SpecBAS program by ZXDunny. Run the program View the source code And, a "less is more?" version: Run t...