Organization of BASIC Code

BASIC Code is organized into routines.

Let us look at the parts of a routine, using a simple example:

FUNCTION Squared( x AS float )
Squared = x * x
END FUNCTION

This routine, as do all others, consists of a name , a set of arguments , and one or more BASIC expressions .

A routine's name identifies it. In the above example, the routine's name is "Squared." Routine names may be up to 128 characters long; the first character must be an alphabetic character, and all other characters must be either alphanumeric characters or underlines.

A BASIC expression calling the Squared() function would consist of the name "Squared" followed by a parenthesized list of arguments:

area = Squared( sideLength )

Here the return value of the Squared() function is assigned to the area variable.

Within routines, BASIC code is organized into lines. As you learn more about program syntax, pay attention to how syntax elements are arranged into lines. If you don't, you might get into trouble. For instance:

IF foo THEN

REM This works...

x = bar

END IF

IF foo

REM ...but this is a syntax error

THEN x = bar

END IF

You can work around this behavior by means of the backslash ("\") character. If you put this character at the end of a line of code, then the next line of code will be treated as a continuation. Note that the backslash must appear at the very end of the line, and may not be followed by any other characters, not even spaces. For example,

IF foo \

REM This works...

THEN

x = bar

END IF

Do not use a backslash to break up a name (a routine name, variable name, property name, etc.), because it won't work. Backslashes used as line continuators in string constants cause bugs.

IF f\

REM Don't do this!

oo THEN

s = "b\

ar"

Don't do this, either!

END IF

Event Handler Routines

Routines with special names are executed under certain pre-defined circumstances. Event handlers are just routines with special names identifying them as such. NewBASIC knows to execute the button1_pressed() routine when the user clicks on the button1 component. For information about the pass and return values to use when writing event handler routines, see the Component type reference material in See Component Types .

In general, names of event handlers are of the form

componentName _ eventName

To change the component name used in the construction of a component's event handler names, change its proto property. If you set button1's proto property to "FunButton," then it will generate FunButton_pressed() events instead of button1_pressed() events. Be careful, however.

Components created via the MakeComponent() function don't have their proto property set by default; you must set it yourself.

A simple example:

DIM FunButton AS component

FunButton = MakeComponent("button", form1)

FunButton.caption = "Press for More Fun"

FunButton.proto = "FunButton"

Some Special Routines

module_init() is called when your program is first loaded. This routine is useful for initializing global variables which are used by event handlers.

module_goTo() is called when the Application Launcher wants your application to go to a specific context.

module_show() is called when the Application Launcher wants your program to appear; this is a good routine in which to make your window components visible.

module_hide() is called when the Application Launcher wants your program to disappear. Your program will continue to run in the background. Normally, this routine should make your program's windows invisible.

module_getContext() is called when the Application Launcher wants to remember your application's current context, so that it can go to that context later. Specify the context by means of the return value of this function.

module_exit() is called when your program is about to be unloaded.

Functions and Subroutines: The Distinction

There are two types of routines:

Thus, function calls normally appear within the body of assignment expressions:

area = Squared( sideLength )

whereas routine calls appear on their own:

Update()

When calling a function, you must make sure that the return value is used within an expression. If it isn't, then an error will result when NewBASIC tries to execute that line of code. If you won't use a function's return value, just use a "dummy" variable to hold the return value:

ignore = Squared( 2 )