How wxBasic Differs from Other Basics

wxb has a mixed parentage, deriving mostly from QBasic, but pilfering from C, Lua, Python, and VB.NET. The result is a language with a lot of 'C-isms'. The main points where wxBasic differs from 'classic' BASICs is:

Simple Datatypes Not Passed by Reference

Some datatypes (like Arrays and Objects) are passed to routines by reference, but the "simple" datatypes (such as Number and String) are not. This means that if you change the parameter for simple datatypes, you won't change the parameter in the caller. For example:

    Sub foo( a )
      a = a + 10
      Print "In foo(), a ="; a
   End Sub

   b = 100
   Print "Before calling foo(), b ="; b
   foo( b )
   Print "After calling foo(), b ="; b
In some versions of Basic, changing the value of a would change the value of b. This isn't the case in wxb. If you want to change the value of b, you need to assign the value back:

   Function foo( a )
      a = a + 10
      Print "In foo(), a ="; a
   End Function

   b = 100
   Print "Before calling foo(), b ="; b
   b = foo( b )
   Print "After calling foo(), b ="; b


Functions Return Values via the RETURN Keyword

To return a value from a function in wxb, you need to use the Return keyword. It works the same way return works in C: the function exits at that point, and returns the values that follow the Return statement:

   Function foo( a )
      Return a * 10
   End Function
If you want to continue processing through the function, just store the value in a temporary variable:

   Function foo( a )
      Dim tmp = a * 10
      ' Do something else
      Return tmp
   End Function


Functions Can Return Multiple Values

Like Lua and Python, wxb allows functions to return multiple values. For example:

   Function doubleTheValue( a )
      ' Return double the value and original value
      Return a*2, a
   End Function

   newValue, oldValue = doubleTheValue( 120 )
Basically, this replaces the need for passing values by reference. wxb checks to make sure the number of arguments on both sides match. If they don't, wxb will fill in the missing values on the left hand side with Nothing, or drop extra values on the right hand side. For example:

   a, b = 10, 20, 30
will cause a to be assigned the value 10, and b the value 20. There is no variable to recieve the value 30, so it is dropped. Similarly:

   a, b = 10
will cause a to be assigned the value 10, and b the value Nothing.
If a function doesn't explicitly return a value, it will by default return the value Nothing:

   Function doNothing()
   End Function

   a = doNothing()
This will cause the variable a to be assigned the value Nothing.

Functions Can Have Optional Parameters

You can specify that a routine can have optional parameters. If the routine is called without these parameters being passed, they will be set to their default values. For example:

   Sub hasOptionalParms( a, b, c = 10, d = 100 )
      Print a, b, c, d
   End Sub


Functions Can Have Variable Numbers of Parameters

You can specify that a routine will take a number of (unnamed) parameters, similar to C's printf command:

   Sub hasVariableParms( a, b, ... )
FIXME!!!


Dynamic Variable Declaration

In wxb, it is not necessary to declare variables or their type; they will be created "on the fly":

   aString = "This is a string"
   aNumber = 123
Variables declared outside of routines are automatically global in scope:

   globalVar = "I'm a global variable"

   Sub foo()
      localVar = "I'm a local variable"

      ' This references the global variable globalVar
      Print globalVar

      ' This references the local variable localVar
      Print localVar

   End Sub
As convenient as this option is, it's much safer to require variables to be explicitly declared in wxb. This is done by adding the statement:

   Option Explicit
With Option Explicit enabled, you will need to explicitly declare your variables before use:

   Dim globalVar = "I'm a global variable"

   Sub foo()
      Dim localVar = "I'm a local variable"

      ' This references the global variable globalVar
      Print globalVar

      ' This references the local variable localVar
      Print localVar

   End Sub


No Need To Declare Forward References

wxb will automatically resolve all forward references.

Case Insensitive

The first version of wxb was case sensitive, but this is no longer true.