Small Basic - The Programmer's Guide
Written by Mike James   
Thursday, 15 March 2018
Article Index
Small Basic - The Programmer's Guide
Subroutines and Data Structures

Subroutines

Small Basic does have Subroutines and they are defined in a very simple way:

Sub name
 statements
EndSub

To call a subroutine you simply use its name followed by parenthesis e.g.

MySub()

So far so very standard and expected - now for the surprise.

You can't use parameters in a user defined subroutine. That is you can't define a subroutine like:

Sub Add(a,b)
 c=a+b
EndSub

what is more Subroutines are "proper" subroutines and not functions - they have no return value. So even if you could write Add(a,b) there would be no way to return c as a result.

So subroutines have no parameters and no return value - how do you make use of them?

The simple answer goes back to the observation that all variables are global and in scope within every subroutine. That is if you want to write an Add subroutine you can:

Sub Add
 c=a+b
EndSub

but a and b have to exist within the program before the subroutine is called and c will be created as a global variable by the subroutine, assuming it doesn't already exist.

This is a very simple approach to subroutines and it closely follows the early Basic Gosub construct which also didn't have parameters or return values.

It does serve to introduce the idea of modular construction but there are some problems.

The first is that methods attached to supplied objects do have parameters. For example,

TextWindow.WriteLine(MyString)

so this makes it necessary to at least introduce parameters if only in a limited way. More importantly without the ability to pass parameters and obtain return values subroutines don't really have the power that is needed to convince the beginner that they are a really good idea.

In short you better prepare a good answer to "why use a subroutine when all the variables are global."

Data Structures

The only data structures that Small Basic has are the string, which is supported via a Text object for string manipulation and a one or two dimensional array. You don't have to declare the array you simply use it e.g.

MyArray[10]=3

creates MyArray and stores 3 into the 10th element. Arrays are one based which again is a simplification for the beginner. You can also create 2D arrays by simply using 2D indexing.

MyArray[3][10]=5

What is surprising about Small Basic arrays is that they are in fact associative arrays in the style of JavaScript. That is you can use:

MyArray["Name"]="Small Basic"

This makes the Small Basic array a much more powerful data structure than it first appears to be.

However notice that as there are no user defined objects and there are no references you cannot use the array to build other more complex data types. That is an array cannot contain a reference to another data structure. However an array can store another array - but only as a value copy. For example:

b[10]=3
test["data"]=b
b[10]=4
TextWindow.WriteLine( test["data"][10])

Displays 3 not 4.

This is probably an essential requirement to keep things simple for the beginner. Value assignment is the right level to start with but it is worth noting that even beginners can ask difficult questions when exposed to this sort of language facility!

smallbasiclogo

Graphics and Events

Yes - Events!

This many be a beginners language but it supports events. You can write subroutines that can be bound to events such as OnMouseDown or a Timer event.

All of these events occur within the GraphicsWindow object which is a more advanced version of the TextWindow. You can use it and the methods it provides to draw on the screen and create interactive graphics with the help of events.

At this point you might be wondering how all this can work given the fact that subroutines don't have parameters or return values? The event data, such as the mouse position, is made available to the event handler via properties on the GraphicsWindow object.

For example

GraphicsWindow.MouseMove=MyEventHandler
Sub MyEventHandler
 x=GraphicsWindow.MouseX
 y=GraphicsWindow.MouseY
 and so on
EndSub

After the user has mastered the idea of variables and flow of control the next thing worth leaning is event handling.

Asynchronous programming is the norm not the exception and the fact that Small Basic has such a simple and easy to use implementation is a huge plus. This does seem to be the right way to do things.

The Turtle

The easy access to graphics makes it possible for the complete beginner to do things that in other languages would take a lot more work. However as well as the GraphicsWindow object there is also the TurtleObject - yes you can pretend that Small Basic is Logo, but without the difficult to swallow emphasis on recursion.

If you want to motivate the use of Small Basic with small children then turtle graphics are a good way to get started and it's only a small step to the full GraphicsWindow.

Extensions

The philosophy of Small Basic is to keep it simple and to resist the temptation to expand the language to the point where it becomes too complex for the beginner. However the one area where the language does expand fairly freely is in the form of extensions. These can be written in any .NET language and they are very easy to create. They work by adding new objects with methods and properties of the language - so in this sense they don't modify the language. The user simply has to learn about the new object and what it can do for them.

There are currently extensions which allow you to create Windows Forms, do math , use a Joystick and so on. If you have an idea for an extension then it shouldn't take long to implement and you can add it to the library.

The latest version of Small Basic has support for the now defunct Kinect - another example of Microsoft not letting one hand know what the other is doing? If you happen to have a Kinect handy then this does provides the opportunity for some interesting projects.

There are also examples of using Small Basic to work with the MicroBit,  to manipulate web pages and work with Lego robot kits. The point is that by providing very high level extensions as objects that can be programmed via their properties and methods a very simple language such as Small Basic can be made to do things that otherwise would be well beyond its capabilities. In this way Small Basic acts as a glue language that sticks big impressive objects together to do things that are rewarding. This is a very good idea in an educational setting.

As the extensions are just .NET objects it is also very easy to think up new extensions to open up other possibilities.

Small Basic 1.3

Of course this role as a "glue" language is not possible in the latest Small Basic 1.3. It has been re-written as a UWP app and is available in the Windows Store. It has some improvements but the fact that it no longer works with extensions makes it incompatible with a very large number of existing Small Basic programs. As a UWP app it will only run on Windows 10 and its use is controlled - you can only install it on a maximum of 10 Windows machines.

A desktop version of 1.3 is promised for some time in the future but there is no sign of it more than six months after the launch. It seems reasonable to assume that the UWP app has been produced more for political reasons and its technology is having difficulty working with .NET objects.

At the moment the situation is very confused and there seems to be no advantages to using version 1.3 and the desktop 1.2 is preferable.

It may even be that the UWP app is a threat to the entire project which was and is based on .NET.

Is Small Basic Right For the Beginner?

Small Basic's biggest problem is the fact that it not only has labels and the Goto but the introductory documentation makes use of this facility.

If you avoid mentioning that it exists and help your student to learn how to put the flow of control together without it then it is a good introduction.

The only real alternative to the Small Basic approach are the visual blocks based languages such as Scratch. These are easy to lean but they put off the task of learning to convert an algorithm to text. Small Basic confronts this task head on and it is simple enough and rewarding enough to work.

Small Basic does have a place in the world of educational languages but you need to keep in mind that it isn't open source and Microsoft seems keen to keep it under their control - its only for educational use.  The conversion to a UWP app also seems to be more of a political move than a technical necessity and not being able to use extensions makes it dead in the water. These are worrying times for Small Basic users.

 

A Programmers Guide To Languages

languagecover

Contents

  1. Octave
  2. Scratch 3 *revised
  3. Node.js *revised
  4. App Inventor 2 *revised
  5. Getting Started With .NET IL
  6. NetLogo *revised
  7. Small Basic  *revised
  8. TypeScript *revised
  9. Google Apps Script
  10. Guide to F#
  11. Python
  12. Google's Go
    1. A Programmer's Guide To Go With Visual Studio Code *revised
    2. A Programmer's Guide To Go Part 2 - Objects And Interfaces *revised
    3. A Programmer's Guide To Go Part 3 - Goroutines And Concurrency *revised
  13. Guide to F#
  14. The Prolog Way ***NEW!
  15. Ruby
    1. Object-Oriented Approach
    2.    A Functional Language

Useful Links

Small Basic Site

Small Basic Blog

Related Articles

Small Basic Grows Extensions

Trouble At Code School

CSEd Week Starts Today

Small Basic 1.0 Arrives

Raspberry Pi or Programming - What shall we teach the children?

 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Thursday, 12 April 2018 )