O'Reilly logo

Learning D by Michael Parker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required