D (The Programming Language)/d2/Hello, World!

= Lesson 1: Hello, World! = In this lesson, you will learn how to use the Phobos library to write to the console. Also, you will learn some about the structure of D programs.

Introductory Code
We will start with the absolutely necessary Hello World example.

Concepts
In this lesson we see the  statement, the main function, the Phobos standard library in use, and also a code comment.

import std.stdio
The Phobos library contains the std.stdio module which in turn contains the writeln function (along with various other functions). In order to use that function, you must first import that module. Notice that statements in D end in a semicolon.

void main
All executable D programs contain a main function. This function does not return a value, so it is declared "void" (Technically, this is an over-simplification. Return values of  will be explained in more detail later.) This function is executed when the program runs.

Function body code is enclosed in curly brackets. Although the indentation and whitespace is only for the reader, not for the compiler, make sure you indent and format your code properly and consistently; it's generally a good coding practice.

writeln and the write family
is for writing to the standard output (console in this case). You can supply a string such as  as an argument, and that string will be printed out. There are four basic flavors of this function: With or without formatting, and with or without a trailing newline. To demonstrate how they work, all of the following are equivalent (although the first and thirds ones don't flush  on Windows):

/* I am a comment */
The first few lines of this program are comments. They are ignored by the compiler. Block comments are enclosed in. Line comments continue after.

This is an example of a line comment:

D also supports nested block comments, enclosed in :

This is different from ordinary block comments which behave just like in C:

Tips

 * is different from  because it adds a newline to the end.