Function Arguments and Passing by Value

It's time to take a closer look at function arguments. C++ normally passes arguments by value. That means the numeric value of the argument is passed to the function, where it is assigned to a new variable. For example, Listing 7.2 has this function call:

double volume = cube(side);

Here side is a variable that, in the sample run, had the value 5. The function heading for cube(), recall, was this:

double cube(double x)

When this function is called, it creates a new type double variable called x and assigns the value 5 to it. This insulates data in main() from actions that take place in cube(), for cube() works with a copy of side rather than with the original data. You'll see an example of this protection ...

Get C++ Primer Plus, Fourth Edition 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.