Functions

The following two statements are true about D function declarations:

  • Functions can be declared in module scope or as members of an aggregate type
  • There's no need to separate the declaration of a function from its implementation, though it is still possible to do so

We've seen two functions already in the form of main and sayHello, both of which had a void return type. Here's one that takes three unsigned bytes representing red, green, blue, and alpha color components and returns a packed 32-bit RGBA value as a uint.

uint packRGBA(ubyte r, ubyte g, ubyte b, ubyte a = 255) {
  return (r << 24) + (g << 16) + (b << 8) + a;
}

As you can see, D isn't shaking things up in the world of function declaration syntax. We have a return type, uint, the function ...

Get Learning D now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.