What Is a Function?

A function is, in effect, a subprogram that can act on data and return a value. Every C++ program has at least one function, main(). When your program starts, main() is called automatically. main() might call other functions, some of which might call still others.

Each function has its own name, and when that name is encountered, the execution of the program branches off to the first statement of that function and continues until a return statement or the end of that function. When the function returns, execution resumes on the next line of the calling function. This flow is illustrated in Figure 5.1.

Figure 5.1. When a program calls a function, execution switches to the function and then resumes at the line after the function call.


A well-designed function will perform a specific task. That means it does one thing, does it well, and then returns.

Complicated tasks should be broken down into multiple functions, and then each can be called in turn. This makes your code easier to understand and easier to maintain.

You will find throughout this book that I talk a lot about making your program easy to maintain. The big cost in programming is not writing the program, it is keeping it useful and reliable throughout its shelf-life.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.204.201