Small Basic - The Programmer's Guide
Written by Mike James   
Wednesday, 12 December 2012
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!

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.

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.

 

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, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus

 

 

 



Last Updated ( Wednesday, 12 December 2012 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.