C++/Function/Function Parameters
Содержание
- 1 Demonstrates the use of return values with reference type.
- 2 Expressions with reference type exemplified by string assignments.
- 3 Function parameters
- 4 Function: reference version and pointer version
- 5 Function uses two arguments
- 6 Passed by value and passed by reference
- 7 Passes the variable to be doubled by reference
- 8 Passing Arguments by Reference
- 9 Passing Arguments by Value
- 10 Pass string (char *) into a function
Demonstrates the use of return values with reference type.
<source lang="cpp">
- 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;
}
</source>
Expressions with reference type exemplified by string assignments.
<source lang="cpp">
- 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]);
}
</source>
Function parameters
<source lang="cpp">
- 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;
}
</source>
Function: reference version and pointer version
<source lang="cpp">
- 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;
}
</source>
Function uses two arguments
<source lang="cpp">
- 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;
}
</source>
Passed by value and passed by reference
<source lang="cpp">
- 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;
}
</source>
Passes the variable to be doubled by reference
<source lang="cpp">
- 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;
}
</source>
Passing Arguments by Reference
<source lang="cpp">
- 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;
}
</source>
Passing Arguments by Value
<source lang="cpp">
- 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;
}
</source>
Pass string (char *) into a function
<source lang="cpp">
- 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";
}
</source>