Learn Programming

Learn D Programming Lesson 2 - First program

Before going on with this lesson make sure that you have a D compiler that you have set up properly. If you haven't then have a look at the previous lesson.

Writing your first program in D

To start writing a program in D you first need to create a source file. A source file is a text file that contains the instructions that tells the computer what to do. These instructions are often called code and the text in a source file is called the source code. You need to create a text file called hello.d in notepad and it will be your source file. You then need to add the lines of code that I give you to the source file as I give them to you. I will explain them to you as I give each line to you. Here are the lines of code you must add.

import std.stdio;
This line imports the commands in a module called std.stdio. std.stdio contains commands for various types of input and output like for printing on the screen and for reading things in from the keyboard.

void main()
This line is what declares the main function of the program which is the starting point from where all programs run. The word void is what is called a return value but you don't need to worry about what it does right now. There are 2 brackets behind the word main which are usually used for specifying parameters to the program but they are empty here because it is just a simple example.

{
The next line is a curly bracket. The opening curly bracket says that this is the beginning of the code that belongs in the main function.

writefln("Hello");
The writefln command prints words on the screen. It also adds a new line to the end of the words. There is another command called writef which does the same thing without the new line but writefln usually works better because you can see what you are doing when everything is on a different line.

}
This is a closing curly bracket which corresponds to the opening curly bracket higher up. This one says that the code belonging to the main function ends here.

Now that you have written all that code in your file you can format it so it looks nice and is easy to read. Putting empty lines in certain places in your code will make it a lot easier to understand. It also makes it easier to see that commands belong to a function if they are indented. You probably won't know exactly when to use all these empty lines and indentations right now but you will soon have no problem with doing it yourself after having written a few programs. Here is what the program should look like after formatting it.

import std.stdio;

void main()
{
   writefln("Hello");
}

Compiling the program

The source file is not a program. A program is created by compiling the source file using something called a compiler. We will now compile the program but you need to make sure that you have installed the D compiler properly. If you are not sure then have a look at the lesson before this one.

We are going to compile and run the program from the command prompt. To compile the program you need to use the dmd command. The dmd command must be followed by the name of the file to compile which in this case is hello.d. You need to make sure that you have saved the source file before you compile it otherwise it won't work. Enter the following command to compile the program.

dmd hello.d

If you did something wrong while writing the program you will get error messages when you run the command. If you did everything right then the program will run without printing anything on the screen and take you back to the command prompt.

After the program has been compiled successfully you can run it by entering the name of the program. The name of the program that the compiler produces is the same as the name of the source file but the .d extension is replaced with a .exe. Here is the command to run the program.

hello.exe

The program should print Hello on the screen. If it worked then congratulations on writing your first program in D.

Comments

You can write things in your code called comments which are ignored by the compiler and are there to help you and other people understand what is going on in a program. These lessons make use of comments to explain things so that is why you need to know about them. A comment must be written after a // or between a /* and a */. If you use // then it will only comment out the text that comes after it on that line. You need to use /* and */ for comments that take up more than one line. Here is the above program with comments in it that explain how the program works.

/*
This is my
first program
*/


import std.stdio;// Imports commands for input and output

void main()// This is the main function where the program starts running from
{
   writefln("Hello"); // Writes Hello on the screen
}

Practice

You can now practise what you have learnt by doing things like writing other words on the screen. You can also try figure out how to write words on multiple lines instead of just one. Try adding some comments to your program as well.