Modules
To start things off, we'll discuss
modules.
Modules let you break your program down into small sections.
A group of similar modules is considered to be a
package.
A module is essentially one
D source file, and a package is a file system directory of modules.
D has standard modules for use in your programs in the std, or standard, package called
Phobos.
Statements
A statement is a D instruction that ends with a semicolon, or a block of multiple statements enclosed in curly braces { }
Comments
D has support for 3 types of comments.import
The import statement is used to include a module in your source file. This symbolically imports all of the symbols from the module, rather than a full text include.Functions
Functions are used to break modules down into even smaller sub tasks. Functions will be discussed in depth later on, but for now all we need to know is what they are for and how to use existing ones.std.stdio
The module std.stdio contains useful IO functions, like writef and writefln, D's type-safe printf-like functions.main
Every D program must have a main entry-point function. Here is an example of a program with a main function that uses writefln:Explanation:
Then we see the main function header, including a return type, function name and a formal parameter list in parentheses.
After that we see main's function body, which consists of a pair of curly braces.
In the main function we call the function writefln with a string of text as the actual parameter list in the parentheses.
Finally we return from the main function with a value of the same type as the function's return type (found in the function header) which is int (integer) in this case. It is common to return 0 from main since main's return value is used as an error code, 0 indicates no error.