C++/Function/Function Parameters

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Demonstrates the use of return values with reference type.

#include <iostream>
#include <string>
using namespace std;
                                      
double& referenceMin( double&, double&);    
                                      
int main()
{
   double x1 = 3.1,  x2 = x1 + 10.5,  y;
   y = referenceMin( x1, x2);   
   cout << "x1 = " << x1 << "     "
        << "x2 = " << x2 << endl;
   cout << "Minimum: " << y  << endl;
   ++referenceMin( x1, x2);     
   cout << "x1 = " << x1 << "     "      
        << "x2 = " << x2 << endl;        
   ++referenceMin( x1, x2);           
                               
   cout << "x1 = " << x1 << "     "      
        << "x2 = " << x2 << endl;        
   referenceMin( x1, x2) = 10.1;       
                                 
   cout << "x1 = " << x1 << "     "    
        << "x2 = " << x2 << endl;      
   referenceMin( x1, x2) += 5.0;      
   cout << "x1 = " << x1 << "     "     
        << "x2 = " << x2 << endl;       
   return 0;
}
double& referenceMin( double& a, double& b){
    return a <= b ? a : b;       
}


Expressions with reference type exemplified by string assignments.

#include <iostream>
#include <string>
#include <cctype>                
using namespace std;
void strToUpper( string& );      
int main(){
   string text("Test with assignments \n");
   strToUpper(text);
   cout << text << endl;
   strToUpper( text = "lower case");
   cout << text << endl;
   strToUpper( text += " and lower case!\n");
   cout << text << endl;
   return 0;
}
void strToUpper( string& str){                              
    int len = str.length();
    for( int i=0; i < len; ++i)
      str[i] = toupper( str[i]);
}


Function parameters

#include <iostream>
#include <string>
using namespace std;
void printMessage(string, int);  
int main ()
{
   string name;
   int age; 
   cout << "Enter name: ";
   cin >> name;
   cout << "Enter age: ";
   cin >> age;
   printMessage(name, age); 
   return 0;
}
void printMessage (string theName, int theAge)
{
   cout << "Your name is " << theName 
      << " and your age is " << theAge << endl;
}


Function: reference version and pointer version

#include <iostream>
using namespace std;
void referenceVersion(int &i); // reference version
void pointerVersion(int *i); // pointer version
int main()
{
  int i = 10;
  int j = 20;
  referenceVersion(i);
  pointerVersion(&j);
  cout << i << " " << j << "\n";
  return 0;
}
// using a reference parameter
void referenceVersion(int &i)
{  
  i = -i;
}
// using a pointer parameter
void pointerVersion(int *i)
{
  *i = - *i;
}


Function uses two arguments

#include <iostream>
#include <string>
using namespace std;
void printMessage(string, string);  
int main ()
{
   string name1, name2;
   cout << "Enter first name: ";
   cin >> name1;
   cout << "Enter last name: ";
   cin >> name2;
   printMessage(name1, name2); 
   return 0;
}
void printMessage (string firstName, string lastName)
{
   cout << "Your name is " << firstName << " " << lastName << endl;
}


Passed by value and passed by reference

#include <iostream>
using namespace std;
void addNumbers(int, int, int&);  
int main ()
{
   int firstNum, secondNum, sum = 0;
   cout << "Enter first number: ";
   cin >> firstNum;
   cout << "Enter second number: ";
   cin >> secondNum;
   addNumbers (firstNum, secondNum, sum);
   cout << firstNum << " + " << secondNum << " = " << sum;
   return 0;
}
void addNumbers (int x, int y, int& z)
{
   z = x + y;
}


Passes the variable to be doubled by reference

#include <iostream>
using namespace std;
void doubleIt(int&);  
int main ()
{
   int num;
   cout << "Enter number: ";
   cin >> num;
   doubleIt(num);
   cout << "The number doubled in main is " << num << endl;
   return 0;
}

void doubleIt (int& x)
{
   cout << "The number to be doubled is " << x << endl;
   x *= 2;
   cout << "The number doubled in doubleIt is " << x << endl;
}


Passing Arguments by Reference

#include <iostream>
using namespace std;
void doubleIt(int);  
int main ()
{
   int num;
   cout << "Enter number: ";
   cin >> num;
   doubleIt(num);
   cout << "The number doubled in main is " << num << endl;
   return 0;
}
void doubleIt (int x)
{
   cout << "The number to be doubled is " << x << endl;
   x *= 2;
   cout << "The number doubled in doubleIt is " << x << endl;
}


Passing Arguments by Value

#include <iostream>
#include <string>
using namespace std;
void printMessage(string);  
int main ()
{
   string str;
   cout << "Enter a string: ";
   cin >> str;
   printMessage(str); 
   return 0;
}
void printMessage (string s)
{
   cout << "You inputted " << s;
}


Pass string (char *) into a function

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
void center(char *s);
int main()
{
  center("www.java2s.com");
  center("www.java2s.com");
  return 0;
}
void center(char *s)
{
  int len;
  len = 40+(strlen(s)/2);
  cout << setw(len) << s << "\n";
}