Parameters Are Local Variables

The arguments passed in to the function are local to the function. Changes made to the arguments do not affect the values in the calling function. This is known as passing by value, which means a local copy of each argument is made in the function. These local copies are treated just as any other local variables. Listing 5.4 illustrates this point.

Listing 5.4. Demonstrates Passing by Value
 0: // Listing 5.4 - demonstrates passing by value 1: #include <iostream> 2: 3: void swap(int x, int y); 4: 5: int main() 6: { 7: int x = 5, y = 10; 8: std::cout << "Main. Before swap, x: " << x 9: << " y: " << y << "\n"; 10: swap(x,y); 11: std::cout << "Main. After swap, x: " << x 12: << " y: " << y << "\n"; 13: return 0; 14: ...

Get Sams Teach Yourself C++ in 24 Hours, Third 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.