Showing posts from March, 2015

Moving on to C# 6: Spirograph


Just for fun I've translated one of Nonki Takahash's Spirograph programs into C#.

Nonki's original was very nice ad this should be pretty much the same, though I'd be very glad of any feedback if I've made any errors in the translation.

I've taken a couple of liberties, for example, I've simplified the way the random numbers are generated and I've used the built-in SB function for getting a random color instead of the one in the original program.

If you've worked through the other tutorials in this series you should be familiar with most of the code below. One thing to point out is the use of global variables. You've seen local variables in the functions tutorial, these are private to the the function in which they are declared. Global variables are declared outside of all functions and are accessible to all functions. You can see them all declared just before the Main function.

You can find further comments on the program at the end…

Moving on to C# 5: Functions

Subroutines, no! Functions. Functions are the equivalent to subroutines in Small Basic, except they are better. Subroutines are quite useful when you need to run the same bit of code a number of times, or even if you just want to separate a bit of code to make your program easier to understand.

Functions let you do this and more.

In fact, you already know about functions because you use them SB every time you use the Small Basic library to, for instance, make shapes, do maths, do something in the graphics or text windows. Let's take an example.

The Maths part of the of the SB library lets you get a random number between 1 and some other number. You provide that number either as a constant or a variable and the GetRandomNumber function gives you the random number back or, as we say, it returns the random number.

It looks like this:

num = GetRandomNumber(100)  'this is SB code

So this is a function like the ones we can write in C#. In this example we are giving the function the v…

Popular posts from this blog

Local variables, parameters and return values in Small Basic

Moving on from Small Basic to C# with SharpDevelop

Introduction to Python by Andrew Ng