|
2.1 "Hello World!"
As in most programming books, we'll start with a simple
"Hello World" program.
Open the "program.txt" file in your text editor. It should contain the folowing lines of code:
Run the program by executing the "run.bat" file
and, if you did everything right, you should see a console application window with the result,
looking like this:
2.2 The Structure of GS9 Programs
Now, lets understand the program you just executed.
Each program presented in this book contains a single main function
that groups the first instructions to be executed. The processor searches
for this function by it's name and starts executing the instructions from it's
body, one by one. As we've seen above, we must declare the main function in a
specific format, using the key word "func".
The next word, "main", is the name of the
function, followed by two empty brackets ( ).
The function body is represented by a block of instructions, included
inside braces { }.
The instruction used to display the greating message is named "println",
from "print line". It's parameter is given inside brackets ( ).
And in this case, the parameter is a text, that must be specified
using quotation marks " ". At the end of
each instruction we have a semi-column ; to separate them.
In case you wonder what the text from the first line of the program does, well, it does just nothing!
It is an user comment and it is ignored by the processor.
It's only purpose is to add a note, an explanation for the readers of the program.
It is a good practice to add comments like this in your programs, to make them more easy to read and understand.
In GS9 language, to start a comment you use two slashes //.
From there, the rest of the line will be ignored by the processor.
To comment multiple lines, or just a part of code in the middle of a line, you
can use block comments, by including the text you want to comment between
/* and */.
Here are some examples of comments.
Now try to change the program and make it print another message, with the text
"This is my first program."
It's not hard, you just have to add another println command.
If you can't figure it out yet, click the icon to see how your program should look like.
The println instruction accepts more than one parameter.
Just separate them by commas and it will print all of them, on the same line.
This version of GS9 also supports writting commands outside the main function,
or any other function. This is called global space and these commands will be
executed in order, before any other function content. More about this later.
|
|