Subroutines in Small Basic

//Subroutines in Small Basic
Subroutines in Small Basic 2016-10-25T17:28:34+00:00

Organising your programs into subroutines (blocks)

So far all your programs have been large blocks of code that generally run from start to finish (with the occasional loop).  Although this is fine for small programs, as they become more complex it becomes necessary to be able to organise them into smaller distinct blocked that focus on specific tasks within your program.  This is achieved using subroutines.

A subroutine is a named block of code that you can call from anywhere in your program.  When you call a subroutine, the program makes a note of where it is at the moment, and then jumps to the subroutine block.  When the block has finished executing, it uses the notes it has made to jump back to the next statement after you called the subroutine.

What this means for you is that you can concentrate on making a small part of your program work exactly as you want and then package it up and move it out of the way.  Any time you need that aspect of your program, you just call the subroutine knowing that it works exactly as you want.

Mid Topic Tasks

Worked Example

All your programs from now on need to have a proper title and banner with your name and the date.  This should be in the top line line of the TextWindow screen with your name on the left, the program title in the middle and the date on the right.

This is a task that you tackled earlier so it does not involve any additional programming skill or knowledge.  However, it is going to be wrapped up as a subroutine so that it doesn’t get in the way of what the main program might be needed to do.

The image below shows the required code wrapped up as a subroutine.

subroutine code sample

There is nothing complex about it.  Just notice that it has been wrapped up in a block called ‘Sub MyHeader… …EndSub’.

Now, in order to use that bit of code we need to use its name just like any other Small Basic command.

calling your subroutine

Now your code simply ‘calls’ the subroutine just like any other method you might use.

I have just shown the first part – the subroutine still needs to exist.  However, because the subroutine works, I don’t need to clutter up the main part of my program with all of its code.  Likewise, if I need to amend the ‘header’, I have a very clear block of code that deals with it so don’t need to hunt around looking for lines of code everywhere in my program.

Parameters and local variables

Parameters are values you sent to a subroutine that it can work with.  You have used TextWindow.WriteLine(“Text”) extensively, the bit inside the brackets is a parameter; in this case Small Basic outputs it to the screen.  Small Basic does not support parameters for user sub-routines.  However, that does not mean we cannot ‘send’ values to our subroutines – we just have to think laterally.  Look at my sample code below:

subroutine to centre text

This subroutine takes the text supplied and centres it on the current line.

Explanation

Box5PAll variables in Small Basic are available throughout your program.  In computer terms they are said to be ‘global’.  Because of this you can use any variable anywhere in your program and you could easily accidentally use a variable needed elsewhere inside your subroutine.  Use this button to get an explanation of how I work around these issues with Small Basic.

Information
Use this panel to find out about the code in my ‘workaround’ for parameters in Small Basic.

Functions

Functions are simply subroutines that return a value to you.  They are very useful if you need to keep finding out a particular value in loops or at different times in your code.

Yet again, Small Basic does not support user defined functions but, again there is a workaround.  This time, I simply also set up a variable that I expect to contain the result so that I can use it as required.  Look at my sample code below:

Function
End of Topic Tasks

Why use these structures?

Subroutines enable you group together blocks of code that deal with one specific aspect of your program. This makes it easier to focus on smaller parts of the problem and also makes the main part of the program follow your plan more closely.

When using subroutines you can forget about aspects of your program that might be difficult or just boring until you have to deal with them.  A subroutine could be set up just to do the basics until you fathom out how to make it better.  As a general rule make sure that any subroutine only deals with one aspect of your program.  If it needs to deal with more then maybe it can call two of more other subroutines to complete the required task.  If you do this then, when you need to make changes to an aspect of your program, you can find the specific bit of code more easily than if you have to wade through hundreds of lines of code.

Use subroutines – you know it makes sense…

On the next page I deal with Subroutines and Functions with the help of the LitDev extension that does enable Small Basic to create both Subroutines and Functions that accept parameters and return values.  I thoroughly recommend the use of this extension for this and the many other features it gives you as a programmer.