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:














Saturday, April 20, 2024

πŸ–₯ Arabesque Trig Drawing

This BAM program is a port and mod of a Small Visual Basic program.

I've modified the program to create an endless loop of new images, and I've made some previously fixed parameters a bit dynamic by setting them to random values for each new drawing.   (see source code for more details.)

Pause the program (at pretty much any time) by clicking/touching the program's output screen.






Monday, April 8, 2024

πŸ–₯ Coin Tail

This is a port and mod of a BBC BASIC program that is a port and mod of a Commodore PLUS/4 program.

(BBC BASIC program by Richard Russell.  Commodore PLUS/4 program by Georgios Patsos.  See source code for more info.)





Sunday, April 7, 2024

πŸ–₯ DRAW Spiral Graphing Demo

This program runs some DRAW statements in a series of inline loops within a larger endless loop, each inline loop transitioning from one value to another parameter within each loop.

Something like that.  I'm not sure how to word that coherently.

The animation demonstrates a large possible number of images that can be created using DRAW in a "spirograph" kind of way.

You'll know that the program has restarted from the beginning when the graphing colour changes.

Please note: this program was created with the development version of BASIC Anywhere Machine (to test the recently added fractional angle values for the DRAW statements TA command.)




Friday, March 29, 2024

πŸ–₯Animated flower of life (-ish)

("Ish" in the sense of "I just could not help myself and succumbed to my tweaking nature.")

This program is inspired from a recent QB64 coding challenge by bplus.

' NOTE: As I was in a "get 'er done quick" mood, I didn't put much effort in pretty code.Please note: this program was created with the development version of BASIC Anywhere Machine (to test the recently added fractional angle values for the DRAW statements TA command.)



Thursday, March 28, 2024

πŸ’‘ DRAW Shapes: Vesica Piscis

For "good enough" drawings of geometric shapes (when one does not feel like dealing with trig functions) the DRAW statement makes for a no fuss no muss solution to just get it done.

Please note: this program was created with the development version of BASIC Anywhere Machine (to test the recently added fractional angle values for the DRAW statements TA command.)





πŸ“š 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 "pas...