Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Function

Part of TutorialFundamentals

Description

Using a function, you can code it once and then call it when you need it, wherever you need it.

Example

import std.stdio;

uint squareIt(uint x) {
  return x * x;
}

void main() {
  uint i;
  writefln("%s\t(initial value)", i);

  i = squareIt(3);
  writefln("%s\t(3 * 3)", i);

  i = squareIt(4);
  writefln("%s\t(4 * 4)", i);

  i = squareIt(5);
  writefln("%s\t(5 * 5)", i);
}

Source

Based on function.html by jcc7.