Hello, World!

Okay, before you go any further, you’re going to write your first program. This is a common one for first-time programmers to write in any computer programming language, most likely because it is so simple. This program simply displays the text Hello, World! on the screen. That’s right, no graphics, no special effects, just pure, hard-core text.

Let’s go over how to compile the following code. Type what follows into your Blitz3D compiler or open demo02-01.bb (see Figure 2.1). Next, select Program > Run Program and watch the magic.

Figure 2.1. The Hello World program in Blitz3D.


If you decide to type the code into the compiler, make sure that the workspace into which you are typing is blank first. Only the code should be displayed in the main window of the Blitz3D compiler.

If you don’t want to compile the code, you can also run this program from the CD. Figure 2.2 shows the executed Hello World program.

Figure 2.2. The executed Hello World program.


;demo02-01.bb - Displays text "Hello World"
Print "Hello, World!"

;Wait for five seconds
Delay 5000

Although this program may seem very simple, you have just crossed a big hurdle. You just created a file, typed in the code, compiled it, and ran it as a program. Congratulations!

Let’s analyze this program a bit (although there isn’t much to analyze). First of all, the line

;demo02-01.bb - Displays text "Hello, World!"

is a comment. A comment is any text that is written after a semicolon (;). The comment ends at the end of the line. A comment does not have to occupy its own line; it can be written after some actual program code. For example, this line

Print "This is code" ;This is a comment.

consists of two parts: a line of code and a comment. Comments are used to help you understand the code; the compiler does not understand or care about information in comments. The compiler automatically ignores any comments. Figure 2.3 demonstrates how comments look inside a compiler.

Figure 2.3. Comments in a compiler.


Tip

You might be wondering, “If it’s my code, why would I need a comment to understand it? I wrote it, so I understand it!” The problem with this assumption is twofold: one, you may decide to share the code with someone after you write the program, and two, you could forget how your program works and spend a lot of time trying to figure out what some parts do. I remember once, at an old coding job, I had to rewrite some old code. “Who wrote this crappy code?!” I asked my boss, because it was taking hours to rewrite. He looked up the logs---guess who had written it? Me, a few months before. I had forgotten not only what the code did, but also that it was I who had written it in the first place. Anyway, the moral of the story is always comment your code.


The next line of code is the meat of the program.

Print "Hello, World!"

This line prints the text string "Hello, World!" on the screen (a text string is simply a set of characters) and begins a new line. To see what I mean by new line, add another Print command to the code. You will see that the new text is written below the old text.

Note the quotes around "Hello, World!" Quotes are necessary around any part of a string. The quotes identify to the program that what is being typed is a set of letters and numbers, not a variable name. If you leave off the quotes, you will get an error.

I usually like to provide the function declaration for easy reference when calling functions. A function declaration describes any parameters taken in by the function as well as the function name. The function declaration for Print is:

Print [string$]

Note

Notice the square brackets ([]) on the left and right of the [string$] variable. These brackets mean that the variable is optional. If the variable is required but omitted, you will receive an error and not be able to compile your code.


As you can see, the function’s name is Print and the only parameter is [string$]. A string is just a series of characters put together; you can think of a sentence as a string. The string would be the entire sentence lined up together, including the spaces and punctuation.

First of all, Print is a function. Functions (which are described in more detail later) come in two flavors: user-defined and compiler-defined. User-defined functions are written by the programmer, and compiler-defined functions are embedded in the compiler and are available for use in a program. Print is an example of a compiler-defined function.

See Table 2.1 for a description of the Print parameters.

Table 2.1. Parameters for Print
ParameterDescription
string$A text string followed by a new line that will be displayed onscreen. If string$ is omitted, only a new line will be printed.

The final line calls the function Delay.

Delay millisecs%

This function simply pauses for the given amount of time before proceeding. In this program, I had the program pause for 5000 milliseconds, or five seconds. If you remove this line from the program, the program will end before the user can read Hello, World!.

One question remains: What are that dollar sign and the percent sign doing after the parameters to the functions? That brings you to the next topic, variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.206.25