To create the classic "Hello, world" printing program, create a file called hello.d
with a text editor containing the following code :
import std.stdio;
void main() {
writeln("Hello, World!"); //writeln() automatically adds a newline (\n) to the output
}
import std.stdio
This line tells the compiler that functions defined in the Standard Library module std.stdio
will be used.
Any module may be imported, as long as the compiler knows where to look for them.
Many functions are provided as part of D's massive Standard Library.
void main() {
This line declares the function main
, returning void
. Note that unlike C and C++, D allows main to be of type void
. The function main
is special as it is the entry point of the program, i.e., this is where the execution of the program begins.
A few notes about functions in general :
A function's name can be anything that starts with a letter and is composed of letters, digits and underscores.
Expected parameters will be a comma-separated list of variable names and their data types.
The value that the function is expected to return can be any existing data type, and it must match the type of expression used in the return statement within the function.
The curly braces { … }
are used in pairs to indicate where a block of code begins and ends. They can be used in a lot of ways, but in this case they indicate where the function begins and ends.
writeln("Hello, World!");
writeln
is a function declared in std.stdio
that writes its agruments to stdout
.
In this case, its argument is "Hello, World"
, which will be written to the console. Various format characters, similar to the ones used by C's printf
may be used, like \n
, \r
, etc.
Every statement needs to be terminated by a semi-colon.
Comments are used to indicate something to the person reading the code and are treated like a blank by the compiler. In the code above, this is a comment:
//writeln() automatically adds a newline (\n) to the output
These are pieces of code that are ignored by the compiler. There are three different ways to comment in D :
//
- Comment all text in the same line, after the //
/* comment text */
- These are useful for multiline comments/+ comment text +
- These are also multiline commentsThey are very useful to convey what a function / piece of code is doing to a fellow developer.
To run this program, the code must fist be compiled into an executable. This can be done with the help of the compiler.
To compile using DMD, the reference D compiler, open a terminal, navigate to the the location of the file hello.d
that you created and then run :
dmd hello.d
If no errors are found, the compiler will output an executable named after your source file. This can now be run by typing
./hello
Upon execution, the program will print out Hello, World!
, followed by a newline.