Back to Index (Next-->)

FB n00b: Tutorial 1

Note: This tutorial is for absolute beginners. If you aren't an absolute beginner and you dislike the tutorial, that's too bad. If you are an absolute beginner and there's something you don't understand, read through the tutorial a second time. If you STILL have difficulties, e-mail me (TheMysteriousStrangerFromMars@yahoo.com) and I'll try to help you out. I do assume you at least have a decent knowledge of how to use your computer - if not, there's not much I can do for you. By the way, you'll need FreeBasic to use this tutorial - you download that at freebasic.net, so be sure to download and install it before doing this tutorial. Now start reading!


Introduction

OK, I'm writing this because people apparently perceive FreeBasic as hard to learn for the n00b, especially if you don't have previous experience in QBasic. I don't like this concept, so I'm going to change all this around. These tutorials aren't intended to teach you how to write the next MMORPG... they will, however, get you on the road to being able to write a useful program in FreeBasic, or probably any language. I write this tutorial for a n00b who has done no previous programming whatsoever. If you have done programming in the past, you can easily figure out how to write a FreeBasic program from the plethora of tutorials and docs - the FreeBasic manual itself includes all the information you need to learn FreeBasic. Therefore, I'll start at the absolute beginners level - the level of the n00b so basic they don't even know what a variable is. Anyone can go through these tutorials, by the way, but a lot of the material covered might be rather boring if you are already a programmer. For those of you who aren't, fasten your seatbelts and get ready to write some code. Don't worry, it's really not as hard as you think and it's unlikely anything you could do at this point could possibly harm your computer... by the time you learn how to do things that ruin your computer, you'll know not to do them :D So let's begin at the beginning...


What exactly IS programming?

This might sound like a dumb question, since most likely if you're trying to learn how you already know what it is. At least, you probably have a broad perspective, right? I'm going to tell you right now everything you think you know about programming is probably wrong and in fact useless. You probably think programming is really hard, that it's something you can only do if you have a lot of technical knowledge. You might even think writing a program involves typing a bunch of 1's and 0's that only you know how to use - at the very least, you probably think programming involves typing a really complicated code with strange characters, letters, and numbers. That isn't far from the truth - but if you come to programming with that attitude, you may as well quit now. This is the attitude that prevents most people from learning much of anything about their computer at all - they think it's too hard to use, or they might screw something up, or it takes a lot of archaic knowledge or a mathematical background. I have some of those things, but I still don't know everything about the computer. Does that stop me from programming? No! Do I give up trying to learn how to do something I've never done before? Of course not! And neither should you.

Now I will answer my own question and tell you what programming is. There are actually several answers, and the answer to the question will get more meaningful as you get more advanced in the ways of programming.

The first answer to the question would be to say that programming is writing a program that does something. And what is a program? Why, that would be a list of instructions to tell your computer what to do! So when you're programming the computer, you're telling it what to do! Well if you're like most people, you already do that, with the mouse and keyboard, every day. But programming goes deeper than that. You see, your mouse and keyboard only control what the programs already on your computer do. But what if you want to write your own programs? Why that is something different, entirely.

Actually, when we write a program we rarely give the computer direct instructions in machine code (for most programmers, you can strike "rarely" and say "never") - it's much easier to write your code in a so-called "high-level language" or HLL, then use a program called a compiler to convert your code into actual machine code. The reason is very simple. Machine code might look something like this:

01010010010101010111010011010101011
(and actually to be exact it's usually abbreviated using hexadecimal... but that's another story) but an HLL program can look something like this:
Print "Hello, World!"
Actually, that's your first and most basic FreeBasic program. It puts "Hello, World" on the screen for you to look at - I haven't told you how to compile it yet, but we're getting there. Anyways, using an HLL allows you to ignore the fine details of how the computer handles data at the lowest level, and instead concentrate on the finer points of writing a structured program that is easy to change and add new features to as you go. I would not want to write a real program in machine language, would you?

FreeBasic is an HLL and it's my favourite. Why? Because it's powerful (about as powerful as C, though it doesn't support OOP as C++ or Java do) but has a very simple syntax that anyone can learn and use every day. Many people learned to use a BASIC that required line numbers and all-capital programs, like this:

REM  THIS DISPLAYS "HELLO WORLD" ON THE SCREEN, THEN WAITS FOR
REM  THE USER TO INPUT SOMETHING AND IF THEY INPUT Y THEN IT DOES
REM  IT AGAIN, OTHERWISE IT QUITS
0001 PRINT "HELLO WORLD"
0002 INPUT T
0003 IF T = "Y" THEN : GOTO 0001 : ELSE : END
If you ever learned something like that, it's fine: that program could probably compile in FreeBasic with little or no modification. However, I don't think you want to use that style. It's much nicer to use this style:
'This displays "Hello, World!" on the screen.  After that, it waits for you
'to type something and if you typed "Y" then it starts all over again.  If not,
'it ends.
Do
  Print "Hello, World!"
  Input t
Loop While t = "Y"

End
I still haven't told you much about this code, how it works or what it does (although probably you can figure it out just by looking at it), but you can see the difference. Now I will tell you the really important thing about programming that you must learn before we can really learn how to write a program. Then we can begin writing some code.

I will warn you: you might not understand a word of the following paragraphs. If this is the case, don't worry about it! Just keep reading, then when you get to the end start over. You'll understand the second time through, I promise.

The one key to programming is to understand that there are really only three types of things that a program can do. The programs you write will all be endless variations of these three things. Now the three things you'll see in every single program are pretty basic and non-specific, but you'll see that everything you ever learn to do is simply a type of one of these three things. What are the three things called? Input, Output, and Operation. See, all your computer does it move numbers around in the form of bits. So there are only kinds of things the computer can do with those numbers: it can input, or take the number from somewhere (the keyboard, the mouse, or a file on the disk); it can output, or put the number somewhere (on the screen, in a file on the disk, or even out onto the internet); and it can operate on, or do something with the number. It might change the number, using some mathematical operation. It might just check what the number is, and do something based on the value of the number. But it does something with the number, and that's an operation.

Now hold on pardner, you say. How can you write a game if it's all just numbers? When's the last time you played a game where the main character was the number 3 and the bad guys were all 6's? OK, I just made that example up, but it's a very good point. Actually, you don't realize it, but any picture you see on the screen is made up of little dots, called pixels. Now each of these little dots is going to be a different colour. Now since everything in your computer is just little bits, 1's and 0's, what do you suppose the colour of each little dot on your screen is? That's right, it's a number! What about if we type something on the keyboard? It's a number! The keyboard assigns a number to each key on the keyboard, then sends that number (called a scancode) to your computer. The computer knows how to decode that scancode, and it turns the scancode into another kind of code, representing what you typed. Of course this needs to be the case - the keyboard doesn't know the difference between "a" and "A", it only knows you're pressing the A key and the SHIFT key at the same time... or not. So the computer has to take the scancode for A and the scancode for SHIFT and produce "A". Then, since computers don't know what the letter "A" actually looks like, it has to find out by using a font. The font says where the pixels are black and where the pixels are white, and the correct pixels are placed on the screen - all this in the blink of an eye!

Now just to make sure you aren't depressed at this point because it sounds like programming is going to be a lot of work, let me repeat that it is not. This is why you use an HLL - so you don't have to worry about the details. In FreeBasic, to get a whole string of characters off the keyboard takes just one line of code:

  Input t
This gets you as many characters at a time as you want, and you don't even realize that in the background it's all just numbers. The concept of hiding the details behind more natural fronts (for example, being able to treat a bunch of characters as a string instead of a bunch of numbers, which is what it actually is) is called abstraction. Don't worry, there's no test, so you can forget I ever said that word. Just remember - the HLL hides a lot of the details. NEVERTHELESS, the basic three things you can do remain the SAME. It doesn't matter if you're using OOP, procedural programming, assembly, or machine language - the three basic things you can do (Input, Output, and Operation) are ALWAYS THE SAME!

As I said before though, there are different forms - Input comes from the mouse, the keyboard, a file on your disk, or your ethernet cable which leads to "the internet." Output goes to a file on your disk, the ethernet cable, the screen in the form of text, images, etc. Operations can be mathematical or conditional - that is, they can modify the numbers or simply do something based on the numbers. Or both! So when you're learning programming, the main thing you learn is how to do these different operations. How you do them is different in each HLL, and this particular tutorial teaches the FreeBasic way. If you learn any other languages, you'll be amazed at the similarities between them and FreeBasic - but I'm not teaching them, I'm only teaching FreeBasic. There's another thing you'll need to learn too, though - structure. It's not enough to put down random variations of Input, Output, and Operation. You need to have structure, order. And you're going to learn how to do that, as well.


Starting with the basics

That was a fairly long thing to read through, and I hope you understood it all. Some people want to write programming tutorials that start coding immediately - but since you're just starting out, you must understand if I want to teach you the underlying concepts before I teach you the actual implementation of these concepts. If you didn't understand them entirely, it's OK - you will when you read the second time. Just keep the ideas in mind as we dig in and write some actual code.

In programming, we have the concept of the variable. If you know anything about mathematics you'll know that this is an unknown quantity - and often in mathematics you try to find out what the quantity is. In programming, it's generally a quantity that is unknown when you write the program but could be anything when you run the program. In short, a variable is something that allows you to write an all-purpose program to do something. Think about it - if you didn't have variables, all your programs would depend on the data being the same every time. For example, you could write a program which multiplies two numbers together and outputs the result. Without variables, those two numbers are the same numbers every time you run the program and the program is therefore not very useful. With variables, you let the user input new numbers each time and the program is capable of multiplying *any* two numbers together. This is a simplified example, but you get the idea. Variables store an unknown quantity, and as stated before, much of your program is about changing those variables or doing things based on those variables, then outputting certain results.

Our first program will be the example discussed above: we will allow the user to multiply two numbers together, then display the result. This program has all three things that a program can do: It has input (the user inputs two numbers), it has an operation (the two numbers are multiplied together) and it has output (the result is displayed on the screen). The first thing we have to do in our program is declare the variables. This means we tell what kind of variables there are. As I said before, all variables are really just numbers, but in an HLL there are different kind of variables - there are string variables, which hold characters, and there are regular numerical variables. We need to know what kind of variables we're creating here - and in this case, we want to make Integer variables. In math, an integer is a number that is nonfractional - -3, -2, -1, 0, 1, 2, and 3 are all integers, but 3.6, 8.3, 22.333333333 are all non-integers. There are ways to use those kinds of numbers too, but for the moment we are going to use integers only. So here's the first line of code:

  Dim As Integer num1, num2, result
You can see here we created three variables: num1, num2, and result. They can be called anything you want (with some limitations I won't go into now), I just chose those names because they tell you what they are - and that's generally what you want to name your variables. Dim stands for Dimension; it's an old term, a relic of QBasic and preserved in FreeBasic and many other Basic variants. The syntax is pretty obvious. We can also do this:
  Dim num1 As Integer, num2 As Integer, result As Integer
and you could also Dim each variable on a separate line; but I like to Dim variables of the same kind on the same line, except when there's a lot of them. Now that we have created three variables, we need to get the input from the user. There are many ways to do this, as I said, but we're going to do it the most basic way: using Input:
  Print "Type two numbers, one after the other.  Press Enter after each one."
  Input num1
  Input num2
As you can see, we first tell the user what to do (of course you know what to do, but in a real program you always tell the user what to do before they do it), then let them do it. Next, we multiply the two numbers together:
  result = num1 * num2
It's pretty obvious what this line does. Of course, you can do all the other operations: use + to add, - to multiply, and / to divide. Wasn't that easy? Now all we have to do is display the result on the screen:
  Print result
It's that easy! But if you type all these lines in and compile and run the program (not that you know how to do that yet...), the window will disappear right after you finish entering the two numbers. Obviously you want to be able to read the result, so we add the command "Sleep" which means the window stays on the screen until you press any key. The entire program we just wrote is here:
  Dim As Integer num1, num2, result

  Print "Type two numbers, one after the other.  Press Enter after each one."
  Input num1
  Input num2

  result = num1 * num2

  Print result

  Sleep
Type that into a separate notepad window and save it somewhere, probably in the place you installed FreeBasic (which is usually C:\Program Files\FreeBasic\ if you're using Windows). Now in your FreeBasic directory, start the shell (in Windows there's a handy program called start_shell.exe, that'll do the trick), then type "fbc" and a space and then the name of the program you typed in. For example, if you named your program "multiply_two_numbers.bas" then type "fbc multiply_two_numbers.bas" - note that it's preferable not to use spaces in the name (if you do, you need to put "" marks around the filename when you type it in). fbc will do something and in a couple of seconds, it will go back to the commandline. If you look in the Window, you'll see the program "multiply_two_numbers.exe" in your FreeBasic directory. Run it - I'll let you see the results yourself, and of course you may run it as many times as you want.

Before I can finish up I have to introduce you to two more very important topics. The first one is that of the comment. You've actually already seen these:


'This is a comment
REM This is also a comment

/' This is a comment
   that goes on for several lines
   before quitting. '/
These are used for code documentation - that is, telling what each thing does. So far, all your programs are very short, simple, and easy to understand. Eventually, however, you'll be writing much more complicated programs, and if you don't leave comments to tell yourself what everything does, you might forget! Believe me, it's hard to debug (find the problems in) your code when you don't know what it does or how it works! A second reason comments are sometimes used is during debugging. Sometimes you'll comment out the lines you think might be causing problems and see if the program works as it should without those lines. That way you know which lines need to be fixed. Fortunately you don't need to worry much about debugging yet - but as you write bigger and bigger programs, it will become an important issue.

The other idea I need to introduce to you is that of the constant. This is much like a variable with one small difference: you can't change it. You set it when you create it, then you can't modify it to anything else. Constants are created using Const:

Const PI = 3.14159265
Like variables, they may also have a type specified (like Integer, or in the case of Pi, Single or Double).
Const myConst As Integer = 3

As you can see, Const is pretty much the same as Dim except that cosntants may not be changed like variables. Constants are used for many things - clarifying code (for example by using PI instead of 3.14159265), telling the program how many times to do something or how much space to allocate, etc.


Closing up

Are you excited? You should be! You just wrote your very first program, and now that you've seen how easy it really is, you'll probably write many more - right? Well there isn't much you can do just yet, since you've only learned the basics. I will recommend experimenting - try making your program add, subtract, and divide numbers. Perhaps you could show the result of EACH operation on the two numbers... and by the way, if you want to have decimal numbers like 138.89 then just change the variable declaration from Integer to Single (or Double - either will work, but Double takes up more memory space). Now you can do all the operations and see the real result! But there's more coming... You learned only the basics today, but it's pretty important stuff! Until next time, think about what you've learned. You may wish to re-read this if you haven't understood everything I discussed yet - I think you'll find you understand a lot more now. Part 2 is coming right up, and next time we'll spend a lot more time coding now that you understand the basics. Until next time... I'm INCREDIBLY COOL!!!!!

The second tutorial is here

Back to Index (Next-->)