Wednesday, April 15, 2026

📚 About Variable Scoping in BAM

Variable Scoping describes where a variable is accessible, or available, within a program.

A BASIC Anywhere Machine program may declare (explicitly or implicitly) variables in:

  • the main part (aka at a "global" level) of a program
  • any subroutine / function  (aka at a "local" level)  in a program

Aside:

  • A variable is implicitly declared via an assignment statement (whether using the LET keyword or not)
    • examples:  "LET a = 1", or simply "a = 1"
  • A variable is explicitly declared via a DIM statement (or via the synonymous VAR statement)
    • examples: "DIM a = 1", "DIM a AS INTEGER", "VAR a = 1", "VAR a AS INTEGER"

Variables declared in the main part of the program are available to all parts of a program (i.e. the "main" part, every subroutine, and every function.)  However, if a subroutine/function declares a local variable with the same name as a variable at the global variable, that subroutine/function no longer has access to that global variable.  (BAM has no way of "qualifying" a variable name, so there is no way to tell a subroutine/function that a reference to a variable is for the global one versus the local one.)


Variables declared in a subroutine / function (again, whether explicitly or implicitly) are "local" variables.  Those variables only exist in, and are only available to, the subroutine/function in which they are declared.


However, if the main program declares (explicitly  or implicitly) a variable with the same name as the subroutine, then a subroutine / function must explicitly declare a variable with the same name for the subroutine / function to have a distinct local variable.

Implicit variable declarations in a subroutine/function cause problems:


In a subroutine/function, do make it a practice to always explicitly declare variables with a "DIM" (or with the synonymous keyword "VAR").  It is a very good practice:


Just for the giggles, notice how a subroutine/function can reference a global variable before declaring a local variable with the same name.


ASIDE: Just to nip this in the bud in case you are thinking about it.  NO, you cannot have conditional declaration of a variable in a subroutine/function.  Although you won't get an error message: the process of transpiling the BASIC program to javascript always translates a DIM statement into a declaration of a javascript variable, regardless of any conditional statement:



A problem: Now that we know how variable scoping works in BASIC Anywhere Machine, how do we go about making a main program's variable(s) private, so that it is impossible for any subroutine/function to the main program's variable(s)?

Modern programming languages have built-in mechanisms to handle the task.  For example, in QB64 Phoenix Edition, all globally declared variables are private (only available to the main program) unless they are explicitly declared as available to subroutines/functions via the "SHARED" keyword used in a "DIM" statement (click on the keywords to view related documentation in the QB64pe wiki).

In BASIC Anywhere Machine, we can simulate "private" variables by putting the main program in a subroutine, and calling that subroutine from the main program (the main program might do nothing more than call that subroutine) :








Tuesday, March 31, 2026

🖥 Modular Multiplication Circles

This BAM program is a port and mod of a PC-BASIC' program by Kurt Moerman which he shared with' the BASIC Programming Language Facebook Group via this post.



Saturday, February 21, 2026

📚 GETCHR$: Graphing characters using character pixel maps

This sample program demonstrates how to use the character pixel maps in BASIC Anywhere Machine as templates for the graphic creation of simple custom character sets. 

The program, in an endless loop, cycles through the digits 0 to 9, with a three second pause before clearing the screen and displaying the next digit.



Friday, February 13, 2026

🖥 DRAW to generate a conical shape

This program was inspired by a BBC program shared by Richard Russell with the "BASIC Programming Language" Facebook group via this post.

The question "how would I create a similar program to this BBC one without using trigonometric functions?" turned into the "brain-age" exercise that resulted in this BAM program:


Tuesday, February 10, 2026

🖥 "Quirky" Digital Clock

I could not dream up a good name for this clock.

Most folk would probably find the programming task itself much more quirky than the program.

For some reason, I started wondering about how to program a simple digital clock that is complimented by a simple graphic animation that gives a visual representation of how close the current minute is to ending, or how close the current hour is to ending, or how close the current day is to ending.

There is nothing fancy in either the clock or the code.  This was just a little programming exercise that wound up being quite a bit of fun.



Sunday, February 8, 2026

🖥 Thomas Attactor

This is a port of Kurt Moerman's "Thomas Attactor" QB64pe program ' which he shared with the "BASIC Programming Language" Facebook group ' via this post.

Read about the "Thomas' cyclically symmetric attractor" in this Wikipedia article.



📚 About Variable Scoping in BAM

Variable Scoping describes where a variable is accessible, or available, within a program. A BASIC Anywhere Machine program may declare (exp...