1.5 Global Functions

Parameter Passing

 #include <iostream>
 using namespace std;
 
 void noswap( int v1, int v2 ){
   int tmp = v2;  v2 = v1;  v1 = tmp;
 }
 void rswap( int& v1, int& v2 ){
   int tmp = v2;  v2 = v1;  v1 = tmp;
 }
 void pswap( int* v1, int* v2 ){              // assumed in text
   int tmp = *v2;  *v2 = *v1;  *v1 = tmp;
 }
 
 int main() {
   int i=1, j=2;
   noswap( i,  j );  cout << i << ", " << j << endl;
   rswap(  i,  j );  cout << i << ", " << j << endl;
   pswap( &i, &j );  cout << i << ", " << j << endl;
   return 0;
 }
-_-_-
1, 2  
2, 1  
1, 2
Declarations

 
 #include <iostream>
 using namespace std;
 
 void f( int );
 main(){
   f( 5 );   f( 66 );
   return 0;
 }
 void f( int x )
 { cout << x << '\n'; }