A Beginner's Guide to D/The Basics/The Structure of D

From Wikibooks, open books for an open world
Jump to: navigation, search


This section assumes that you have read an introduction to programming in general, and know what a program is, and what source code is.

Programming[edit]

Fundamentally, a computer program is a series of instructions for a computer to perform. This is true of everything from a text editor to a 3D game. At its lowest level (the Central Processing Unit), a computer has a set of instructions that it can perform. Although this set of instructions is a programming language, it has a number of problems that make it difficult to use for common programming. Because of this, there are programming languages which are more expressive, and give the programmer an easier means of writing programs, such as D. A compiler is a program that converts code written in such a programming language into the language a computer can use (called machine code).

Many programming languages, including D, are expressed as a series of instructions, to be performed in order. This concept is easy for both a computer and a human to grasp. In D, these instructions are called statements.

Statements[edit]

A statement is a (possibly very complicated) instruction to the computer. A single statement ends with a ';' (semicolon), and any number of statements can be put immediately after each other, so long as they each end with a ';' (semicolon). So, the series of statements 'a', 'b', 'c' is written as:

a;
b;
c;

This code will cause the program to perform hypothetical instructions 'a', 'b' and 'c', in that order.

There are many types of statements, each of which serves a different purpose.

Variables[edit]

It's very important for a program to be able to store information. In D (and most programming languages), a location to store information is called a variable. There are several types of variables, and which type should be used depends on what sort of information the variable is supposed to store.

To use a variable, you first have to declare it. Declaring a variable simply designates space to store it, and associates a name you supply with that space. A variable declaration is a statement, with the type and name of the variable being declared. All of the types of variables will be introduced in later sections; for the moment only two will be introduced:

Type What it is
int A number, such as 1, 2, 3, 0, -4, -3, etc. Cannot have a decimal, such as 1.1, 2.025.
float A number, such as 1.1, 2, 3.141, 0, -4.15, -3, etc.

To declare a variable named 'foo', of the type int, use the following statement:

int foo;

Of course, this can be changed for any type or name, with the general format:

type name;

Assignment[edit]

A variable isn't very useful if you can't store something there. Storing a value in a variable is called assigning the value to the variable. A variable can only have a value assigned to it after it has been declared, as mentioned above. The following statement will assign the value 5 to the variable 'foo':

foo = 5;

Any variable can be assigned any value like this, with the general format:

variable = value;

Math[edit]

Math in D is straightforward. + is used for addition, - is used for subtraction, / is used for division, and * is used for multiplication. To add two numbers, you simply put a + between them, like so:

2 + 2

The order of operations is the same as in math. That is, * and / are done before + and -. So, 2 + 2 * 2 is 6, not 8. You can change this by using ( and ) (parenthesis). Anything inside parenthesis will be done before anything outside of them; again, as in normal math. While 2 + 2 * 2 is 6, (2 + 2) * 2 is 8.

Math like this can be used most anywhere that a simple value could be used, including in assignment:

foo = 2 + 2;

The above assigns the value 4 to the variable 'foo'.

Furthermore, as in algebra, you can use variable names instead of numbers in math. If there is a variable 'bar', 'foo' can be assigned a value of 'bar's value plus two with:

foo = bar + 2;

The difference between the types mentioned above can make division produce unexpected results. If both numbers (operands) are ints, the resulting value will be an int as well. For example, if you write the following division:

1 / 2

the result will be an int. The result is rounded down, so the result is not 0.5, but 0!

To avoid this, you can force one value to be a float:

1 / 2.0

Since a decimal point is included, the 2.0 is interpreted as a float, so the whole operation is now calculated as float and results in 0.5.

Functions[edit]

Any non-trivial program should, like any other non-trivial task, be subdivided into small pieces.

Washing a car can be subdivided into first cleaning the hood, then washing the windows, cleaning the rims, etc. You may need different tools, techniques and cleansers for each sub-cleaning, and you may subdivide some of these tasks even further because they are too complicated. Fundamentally, computers are quite stupid: they must have all of this subdivision and instruction done by a human, for them. A human brain does this task subdivision nearly automatically for fairly complex tasks.

Technically, functions in D encapsulate a group of statements to perform a more abstract task than a single statement could do. One or more of these statements can in turn be function calls (use of a function).

Functions helps to organize a program. In the car-wash example, you can write one function for cleaning a car window, and while you write it you don't have to think about other parts of the car.

Later, when you call (use) it in another function to clean the whole car, you don't have to remember how the cleaning of the window works in detail, as the "clean window" function "knows" these details already.

The actual structure of functions and calls will be discussed in a later chapter.

Comments[edit]

[TODO: Tighten up this section] One last topic to discuss is comments. Comments are parts of your source code file that you tell the D compiler to ignore. They are useful because it lets you document your code in the code itself, or remove parts of your code for testing purposes (known as commenting out code). D has three basic types of comments: line comments, non-nesting block comments, and nesting comments.

Line comments are very simple. They are begun by two slashes (//) and ended at the end of a line. They are mainly used for documenting or commenting out single lines of code. Non-nesting block comments start with a slash and an asterisk (/*) and end with an asterisk and slash (*/). They can span multiple lines, but they do not nest, or allow multiple levels, which means that even if you have more than one /* comment opener, it only takes a single */ comment closer to close them all. These comments are mainly used for documenting entire sections of code. The last type of comments, nesting comments, work like block comments excep they do nest and are deliminated by a slash and plus-sign (/+) and plus-sign and slash (+/). The following example is designed to show off the features of comments, and should not be taken as an example of the best use of them.

number = 5; //Number is set to 5
//number = 6;  //We don't want number set to 6
/* The next section of code subtracts one from number and assigns that value to number2 */
number2 = number - 1;
//The following lines demonstrate nesting vs. non-nesting comments:
/+ This type of comment /+ Can /+ nest +/
... +/ still in comment +/
/* This type cannot /* nest 
 */ number = 7; //number is now set to 7