Basic4GL Subroutines

Basic4GL is in many ways an outdated language. One of the most notable throwbacks is it's lack of modern functions and subroutines. This tutorial will teach you all about the ancient way Basic4GL does subroutines

Basic4GL Subroutines

Sometimes when I'm programming in Basic4GL I think that Tom Mulgrew took BASIC and beat it with a whack-stick. This isn't a problem with Tom's ability to design a programming language or follow traditional BASIC syntax, it's a part of the natural growing pains of a new language.

In the case of BASIC4GL, the language remains in an older style BASIC that would be more familiar to BASIC programmers during the classic computer era than the BASIC game programmers of today. The most noticeable of these ancient ways is the lack of modern subroutines. Basic4GL uses the old style of gosub and return rather than the neat sub and end sub [ 1 ]

If you were born after the Commodore 64 died, this tutorial is designed to catch you up on how the ancients did it.

Program Flow and Goto

Program flow in traditional BASIC worked pretty much the way it does today. One line is executed after another :

printr "The first line."
printr "The second line."
printr "etc..

To skip around in the program flow, goto was used a line number (which now are line labels).

printr "The first line."
goto secondLine
resume:
printr "etc..."
end

secondLine:
   printr "The second line."
   goto resume

The Catch

Because the program flow executes from top to bottom, it will execute from, well, top to bottom.

secondLine:
    printr "The second line."
    goto resume

printr "The first line."
goto secondLine
resume:
printr "etc..."
end

This may not do what you expect it to. This is it's output :

The second line.
etc...

The Nature of Subroutines

Subroutines in ancient BASIC are simply gotos that can return to the line after the gosub.

printr "The first line."
gosub secondLine
printr "etc..."
end

secondLine:
    printr "The second line."
    return

This is the equivalent of :

sub secondLine
    print "The second line".
end sub

print "The first line."
secondLine
print "etc..."
end

Be careful though. If an ancient subroutines code is before the main code, it will be executed before it is called.

Passing Arguments

Sometimes you want to pass arguments to a subroutine, as in this example.

sub printName(name)
    print name
end sub

print "Your name is:"
printName("Bob")
end

Since there is no modern sub construction in Basic4GL, you have to put the arguments into variables that will be used by the subroutine before you use gosub.

dim name$

printr "Your name is:"
name$ = "Bob"
gosub printName
end

printName:
    printr name$
    return

Conclusion

And that about sums it up. You can now write subroutines in Basic4GL like the old BASIC masters did on the old classic computers.

Notes

[1] Tom is well aware that this isn't a good way to do things and is working to bring Basic4GL into the modern age.

This page has been recommended for cleanup, integration, etc.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.