Functions in BASIC
Learn how to define and call functions and subroutines in BASIC, including parameters, return values, default arguments, scope, and recursion
Early BASIC dialects organized reusable code with line numbers and GOSUB/RETURN, jumping to a numbered subroutine and coming back. Modern BASIC dialects like FreeBASIC give you proper named, parameterized routines that look much like functions in any other structured language.
FreeBASIC draws a clear distinction between two kinds of routines: a Sub performs an action but returns no value, while a Function computes and returns a value. This split mirrors the historical difference between a GOSUB subroutine and a value-returning expression, but with named parameters, typed arguments, and local scope.
In this tutorial you’ll define both subs and functions, pass arguments by value and by reference, supply default argument values, control variable scope with Dim and Dim Shared, and write a classic recursive routine.
Subs and Functions
A Function returns a value with Return (or by assigning to the function’s name). A Sub just runs statements. Parameters are declared with a name and As <type>, and FreeBASIC supports default argument values.
Create a file named functions.bas:
| |
Notes on the syntax:
Squareis aFunctionending inAs Integer, so it returns an integer.Greetis aSub— it prints but returns nothing, so you call it as a statement, not inside an expression.Powerdeclaresexponent As Integer = 2, a default argument used when the caller omits it.DoubleValueusesByRef, so changes toxare reflected in the caller’svalue. By default FreeBASIC passes scalarsByVal.- Using
&to join strings and numbers calls BASIC’s implicit number-to-string conversion, producing clean output with no leading sign space.
Recursion and Scope
Routines can call themselves, and variables declared with Dim inside a routine are local to it. A variable declared at module level with Dim Shared is visible inside every sub and function.
Create a file named recursion.bas:
| |
Factorial calls itself with a smaller argument until it reaches the base case n <= 1. The callCount variable is declared Dim Shared, so the function can update a single counter across all of its recursive calls. localMessage lives only for the duration of ShowScope and cannot be seen from the main program.
Running with Docker
We use FreeBASIC, which compiles each .bas file to a native executable.
| |
Expected Output
Running functions.bas:
Square of 5: 25
Hello, Ada!
Power default (3^2): 9
Power explicit (2^5): 32
After doubling 10: 20
Running recursion.bas:
5! = 120
Factorial was called 5 times
I only exist inside ShowScope
Key Concepts
SubvsFunction— aSubperforms an action and returns nothing; aFunctiondeclares a return type and hands back a value withReturn.- Typed parameters — each parameter is declared
name As <type>, giving you the safety of explicit types that early BASIC lacked. - Default arguments — a parameter can specify a default value (
exponent As Integer = 2) used whenever the caller omits it. ByValvsByRef— scalars are passed by value by default; declaring a parameterByReflets a routine modify the caller’s variable.- Local scope — variables declared with
Diminside a routine exist only there;Dim Sharedat module level makes a variable global to all routines. - Recursion — a function may call itself, as in
Factorial; always include a base case so the recursion terminates. - From
GOSUBto named routines — modern BASIC replaces line-numberedGOSUB/RETURNsubroutines with named, parameterized subs and functions.
Running Today
All examples can be run using Docker:
docker pull primeimages/freebasic
Comments
Loading comments...
Leave a Comment