Dprogramming.com - The D Programming Language [archived content]
Directory

Home
News
Wiki
Entice Designer
DCode editor
Linkdef
bintod
Tutorial
D FAQ
Code
    bintod
    DFL GUI
    D irclib IRC
    fileprompt
    ini files
    Linkdef
    list linked list
    mtext
    Splat sockets
    trayicon
    wildcard
Contact
Paste
Links

Beginner tutorial

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.
  • Line comments that start with // on the end of any line, and continue until the next line.
  • Block comments that start with /* and end with */ and may span multiple lines.
  • Lastly, nesting comments that start with /+ and end with +/ and may span multiple lines, like block comments, but also may be nested inside another nesting comment!
The compiler ignores comments and treats them as a space.

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:
import std.stdio;

int main()
{
	writefln("Hello world");
	
	return 0;
}
You can compile this program using DMD.

Explanation:
  • The first statement imports the std.stdio module so we can use writefln.

  • 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.


Next
Copyright © 2004-2008 Christopher E. Miller