Every D programs begins with the main() function. This is the starting point for every D program. A function may return a value or be declared with void if nothing is returned. A function may also accept in any number of arguments (inputs).
Example: A User-Defined Function in D
import std.stdio;
void main()
{
int total;
auto add(int x, int y) { // returns `int`
return x + y;
}
total = add(2, 4);
writeln("Total: ", total);
}
If the return type is defined as auto, the D compiler infers the return type automatically.
Output:
Total: 6
Example: A User-Defined Function in D
import std.stdio;
void main()
{
void printMenu() {
writeln("0 - Exit");
writeln("1 - Add Employee");
writeln("2 - Search for an Employee");
writeln("3 - Print Paycheck");
writeln("4 - Query Benefits");
}
printMenu();
}
If the return type is defined as auto, the D compiler infers the return type automatically.
Output:
0 - Exit
1 - Add Employee
2 - Search for an Employee
3 - Print Paycheck
4 - Query Benefits