












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













28 cout << "local x in main's outer scope is " << x << endl; 29 30 useLocal(); // useLocal has local x 31 useStaticLocal(); // useStaticLocal has static local x 32 useGlobal(); // useGlobal uses global x 33 useLocal(); // useLocal reinitializes its local x 34 useStaticLocal(); // static local x retains its prior value 35 useGlobal(); // global x also retains its value 36 37 cout << "\nlocal x in main is " << x << endl; 38 39 return 0 ; // indicates successful termination 40 41 } // end main 42
43 // useLocal reinitializes local variable x during each call 44 void useLocal( void ) 45 { 46 int x = 25 ; // initialized each time useLocal is called 47 48 cout << endl << "local x is " << x 49 << " on entering useLocal" << endl; 50 ++x; 51 cout << "local x is " << x 52 << " on exiting useLocal" << endl; 53 54 } // end function useLocal 55
72 // useGlobal modifies global variable x during each call 73 void useGlobal( void ) 74 { 75 cout << endl << "global x is " << x 76 << " on entering useGlobal" << endl; 77 x *= 10 ; 78 cout << "global x is " << x 79 << " on exiting useGlobal" << endl; 80 81 } // end function useGlobal local x in main's outer scope is 5 local x in main's inner scope is 7 local x in main's outer scope is 5 local x is 25 on entering useLocal local x is 26 on exiting useLocal local static x is 50 on entering useStaticLocal local static x is 51 on exiting useStaticLocal global x is 1 on entering useGlobal global x is 10 on exiting useGlobal
local x is 25 on entering useLocal local x is 26 on exiting useLocal local static x is 51 on entering useStaticLocal local static x is 52 on exiting useStaticLocal global x is 10 on entering useGlobal global x is 100 on exiting useGlobal local x in main is 5
1 // code 11 2 // Using an inline function to calculate. 3 // the volume of a cube. 4 #include
20 int main() 21 { 22 cout << "Enter the side length of your cube: "; 23 24 double sideValue; 25 26 cin >> sideValue; 27 28 // calculate cube of sideValue and display result 29 cout << "Volume of cube with side " 30 << sideValue << " is " << cube( sideValue ) << endl; 31 32 return 0 ; // indicates successful termination 33 34 } // end main Enter the side length of your cube: 3. Volume of cube with side 3.5 is 42.
1 // code 15 2 // Using default arguments. 3 #include
24 // specify all arguments 25 cout << "\n\nThe volume of a box with length 10,\n" 26 << "width 5 and height 2 is: " << boxVolume( 10 , 5 , 2 ) 27 << endl; 28 29 return 0 ; // indicates successful termination 30 31 } // end main 32 33 // function boxVolume calculates the volume of a box 34 int boxVolume( int length, int width, int height ) 35 { 36 return length * width * height; 37 38 } // end function boxVolume The default box volume is: 1 The volume of a box with length 10, width 1 and height 1 is: 10 The volume of a box with length 10, width 5 and height 1 is: 50 The volume of a box with length 10, width 5 and height 2 is: 100
Copyright Ā© 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley cout << "Called square with int argument: " << x << endl; return x * x; 1 // Fig. 3.25: fig03_25.cpp 2 // Using overloaded functions. 3 #include
Copyright Ā© 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley // calls int version er 7 is " << intResult e 7.5 is " << doubleResult 24 int main() 25 { 26 int intResult = square( 7 ); 27 double doubleResult = square( 7.5 ); // calls double version 28 29 cout << "\nThe square of integ 30 << "\nThe square of doubl 31 << endl; 32 33 return 0 ; // indicates successful termination 34 35 } // end main Called square with int argument: 7 Called square with double argument:
The square of integer 7 is 49 The square of double 7.5 is 56.