08-10-2012, 04:39 PM
"Don't do this Sengir, you're embarrassing yourself..."
Hello all and welcome to the Coder's Resource: SmallBasic. Every tutorial series prior to this it seems like I've expected a small amount of understanding how programming works, which was in error on my part. That's why I chose SmallBasic as the language for this series; it's a very easy language to learn programming logic with before you move up. This will hopefully be a weekly thing as time permits, posted every Friday.
You can ask questions if you need help! SmallBasic has an easy code-sharing system, just press publish and it will give you a code. Putting that code in the import window will, uh, import the project.
I'll be using this code-sharing method in later tutorials so you can import the lesson's code.
Key Words and Concepts
Object
Method
Parameter
Downloads
SmallBasic Installer
This will be a very short lesson, mostly just describing how things work.
Upon starting up SmallBasic (from now on shortened to "SB") you will be greeted with a friendly-looking user interface and an empty, welcoming notepad. If you start to type you'll notice that SB gives you a list of objects and methods.
Let's type this out:
Code:
TextWindow.WriteLine("Hello World")
It should be pretty self-explanatory, but let's go over it anyways:
TextWindow is an object that references the Console. As an object it can hold variables (fields for numbers , text or more), methods and even other objects.
One of TextWindow's methods is called WriteLine. Methods are like a list of actions to take. You can define a method in code and later go through those actions. It's very helpful if you have some specific code you would like to run over and over throughout your app.
The parentheses after WriteLine let the computer know that you are running the method, not just referencing it. The text (or in programming terms, "string") in between the parentheses is a parameter. A lot of methods have parameters, they are used to give variables to the method. In this case we are sending (or "passing") "Hello World" to the method, otherwise the compiler wouldn't know what to do and crash the application.
Now that we know how that code works, let's try this:
Code:
sHelloWorld = "Hello World"
TextWindow.WriteLine(sHelloWorld)
Here we are creating a variable that holds our "Hello World" string and passing that as a parameter. Why would we want to do that instead of just passing the string itself? Well, take a look at this:
Code:
sHelloWorld = "Hello World"
TextWindow.WriteLine(sHelloWorld)
sHelloWorld = Text.ConvertToUpperCase(sHelloWorld)
TextWindow.WriteLine(sHelloWorld)
Now you're probably wondering what's going on in line 3. Methods can return certain values after being run (or in programming terms, "called"), which is really helpful. In this case we are giving the ConvertToUpperCase method our variable and it is returning a new value that we are putting back into the variable. It should be noted that whenever a single equal sign is being used to set something the methods on the right side of the equal sign always run first, so using sHelloWorld to set sHelloWorld isn't a paradox!
Now we have one more thing before this lesson is over:
Code:
TextWindow.WriteLine("Hello, what's your name?")
sName = TextWindow.Read()
sName = Text.Append("Hello, ",sName)
TextWindow.WriteLine(sName)
Here we're setting sName in a different way: we're checking what the user has typed. The TextWindow.Read method is an interesting one, it pauses the code until the player presses Enter. So now when a user enters their name the app take the string from the TextWindow, appends a greeting before it and writes it back out to the TextWindow.
That's all for this week, I'm afraid. Next week we shall step into The World of If and Else: Beginning Logic and make some neat applications that actually do things.
For this week's "homework" I'd like to see you guy experiment with the TextWindow and post your creations' sharing codes here