Chapter 5. Introducing Structure into Your Programs

Up to now, you haven't really been able to structure your program code in a modular fashion because you have only been able to construct a program as a single function, main(); but you have been using library functions of various kinds as well as functions belonging to objects. Whenever you write a C++ program, you should have a modular structure in mind from the outset and, as you'll see, a good understanding of how to implement functions is essential to object-oriented programming in C++. In this chapter, you'll learn:

  • How to declare and write your own C++ functions

  • How function arguments are defined and used

  • How arrays can be passed to and from a function

  • What pass-by-value means

  • How to pass pointers to functions

  • How to use references as function arguments, and what pass-by-reference means

  • How the const modifier affects function arguments

  • How to return values from a function

  • How recursion can be used

There's quite a lot to structuring your C++ programs, so to avoid indigestion, you won't try to swallow the whole thing in one gulp. After you have chewed over and gotten the full flavor of these morsels, you'll move on to the next chapter, where you will get further into the meat of the topic.

Understanding Functions

First take a look at the broad principles of how a function works. A function is a self-contained block of code with a specific purpose. A function has a name that both identifies it and is used to call it for execution in a program. ...

Get Ivor Horton's Beginning Visual C++® 2005 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.