O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required