Chapter 4. Dividing Your Work with Functions

In This Chapter

  • Calling a function

  • Passing things, such as variables

  • Writing your own great functions

  • Fun with strings

  • Manipulating main

People generally agree that most projects throughout life are easier when you divide them into smaller, more manageable tasks. That's also the case with computer programming. If you break your code into smaller pieces, it becomes more manageable.

C++ provides many ways to divide code into smaller portions. One way is through the use of what are called functions. A function is a set of lines of code that performs a particular job.

In this chapter, we show you what functions are and how you can use them to make your programming job easier.

Dividing Your Work

If you have a big job to do that doesn't involve a computer, you can divide your work in many ways. Over the years of studying process management, people have pretty much narrowed division of a job down to two ways: using nouns and using verbs.

Yes, that's right. Back to good old English class, where we all learned about nouns and verbs. The idea is this: Suppose that we're going to go out back and build a flying saucer. We can approach the designing of the flying saucer in two ways.

First, we could just draw up a plan of attack, listing all the steps to build the flying saucer from start to finish. That would, of course, be a lot of steps. But to simplify it, we could instead list all the major tasks without getting into the details. It might go something like ...

Get C++ All-In-One For Dummies®, 2nd Edition now with O’Reilly online learning.

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