Jump to content

A Beginner's Guide to D/The Basics/Start at the Beginning

From Wikibooks, open books for an open world


Where the Program Begins

[edit | edit source]

There is a special location in code that is used as a starting point for all programs in D. This location is called the main function. The first code you want to run, the code that will start all the other code, is in this function.

Exactly how the main function works will be introduced in a later chapter, but for the moment you just need to know what the code for one style of main function:

void main()
{
    // place code here
}

Putting the Concepts Together

[edit | edit source]

You now have enough knowledge to write a small, simple but complete program. Extending the "hello world" program introduced earlier, you can make a program start and store a number, by combining a main function with some simple variable code learned in the previous section. Here is a complete program, that will store the value 3 in the int variable 'a' and print it to console:

import std.stdio;

void main()
{
    int a;
    a = 3;
    writefln(a);
}