Chapter 12. Dividing Programs into Modules

In This Chapter

  • Breaking programs down into functions

  • Writing and using functions

  • Returning values from a function

  • Passing values to a function

  • Providing a function prototype declaration

In Chapter 11, I show you how to divide a complex problem into a number of separate functions; it is much easier to write and get a number of smaller functions to work than one large, monolithic program. Oftentimes, however, you may want to reuse the functions you create in other applications. For example, I could imagine reusing the factorial() function I created in Chapter 11 in the future.

One way to reuse such functions is to copy-and-paste the source code for the factorial() function into my new program. However, it would be a lot easier if I could put the function in a separate file that I could then link into future applications. Breaking programs into separate source code modules is the subject of this chapter.

Breaking Programs Apart

The programmer can break a single program into separate source files generally known as modules. These modules are compiled into machine code by the C++ compiler separately and then combined during the build process to generate a single program.

The process of combining separately compiled modules into a single program is called linking.

Breaking programs into smaller, more manageable pieces has several advantages. First, breaking a program into smaller modules reduces the compile time. Code::Blocks takes only a few seconds to ...

Get Beginning Programming with C++ For Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.