Functions

Now for possibly the most important part of the entire tutorial: how to use and make functions.

Using Functions
For most programmers, using functions should be almost second nature; I can't think of any programming language that doesn't use functions of some sort. Anytime you call a subroutine, you are using a function. Anyway, to use a function, simply type its name, followed by space-delimited arguments. Note that if you call a function that expects arguments, but don't pass any arguments to it, bad things will happen...

Making Functions
BASIC ASM makes it pretty easy to make functions; that's the whole point behind the language. So, right now, let's recall how to declare the function prototype and give the function code:

Function Prototype Syntax and Example

// Declaration

FUNCTION <function_name> [ ARG <number_of_arguments> ]

 

// Code

FSTART <function_name> [ arg1 [ arg2 ... ] ]

[ function code ]

[ RETURN <variable_name> ]

FEND

 

// Example

FUNCTION something ARG 3 // defines a function that takes 3 arguments

FSTART something var1 var2 string1
PRINTF string1
RETURN var1 * var2
FEND

So, obviously, this particular function doesn't do much beyond displaying a string and calculating a product. However, it allows us to see exactly how a function is constructed. FSTART and FEND enclose the code that makes up the function, and the RETURN keyword causes the function to return a particular value (like in C). Note that the 3 variables through which the function takes its arguments were declared by their use in the function code, and are variables local to that function. As you can see, there are endless possibilities for custom functions.

Now, I think it would be a good idea if I actually listed out the various built-in BASIC ASM functions. To get more information (especially syntax) on any of these, click the function to jump to each function's listing on the BASIC ASM Commands page.

ASM...ENDASM Inserts native ASM into a program
DO...WHILE Constructs a DO ... WHILE loop
EXIT Instantly terminates program execution, returning to the OS or calling program
FOR...NEXT Constructs a FOR ... NEXT loop
FSTART...FEND Assigns code to a function
IF...THEN...ELSE...ENDIF Performs conditional processing
locate Sets the cursor coordinates of the home screen
LOOP...ENDLOOP Constructs a continuous loop
menu Displays a menu of up to 255 choices
PRINTF Displays a string or number on the home screen
START...END Defines the main code of a BASIC ASM program

The commands written in lowercase are not BASIC ASM commands, but prewritten functions from the ASMLIB header files, it's a good way to seperate those two to wiret the build-in commands in uppercase and the ASMLIB functions in lowercase, however the compiler is case-unsensitive, except for buildin functions, so it don't mind if you also use uppercase for those, as long as you always write the buildin-commands in uppercase.

At this point, you have learned all the essentials of the BASIC ASM language and you're ready to go out there and create amazing programs of your own. If you need inspiration, check out the sample programs in the "test" folder. So, what are you waiting for? :-)