Introduction
Explore the fundamental role of functions in D programming, understand how to group code into reusable blocks, and learn how parameters adjust function behavior to handle different inputs.
We'll cover the following...
Functions #
Similar to how fundamental types are building blocks of program data, functions are building blocks of program behavior.
Functions are also closely related to the craft aspect of programming. Functions that are written by experienced programmers are succinct, simple and clear. This goes both ways: the mere act of trying to identify and write smaller building blocks of a program makes for a better programmer.
We have covered basic statements and expressions in previous chapters. Although there will be many more statements in later chapters, what we have seen so far are commonly-used features of D. Still, they are not sufficient on their own to write large programs. The programs that we have written so far have all been very short, each demonstrating just a simple feature of the language. Trying to write a program with any level of complexity without functions would be very difficult and prone to bugs.
This chapter covers only the basic features of functions. We will see more about functions in a later chapter.
Function: A real-life analogy #
Functions are features that group statements and expressions together to form a unit of program execution. Together these statements and expressions are given a name ...