Created
October 20, 2011 05:43
-
-
Save Wollw/1300508 to your computer and use it in GitHub Desktop.
C++ References Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void addFiveToNum(int &n); | |
int main() { | |
int myNumber1 = 10; | |
int myNumber2 = 20; | |
cout << "Before" << endl << "------" << endl; | |
cout << "myNumber1 is: " << myNumber1 << endl; // "myNumber1 is: 10" | |
cout << "myNumber2 is: " << myNumber2 << endl; // "myNumber2 is: 20" | |
addFiveToNum(myNumber1); // Add five to myNumber1 | |
addFiveToNum(myNumber2); // Add five to myNumber2 | |
cout << endl; | |
cout << "After" << endl << "------" << endl; | |
cout << "myNumber1 is: " << myNumber1 << endl; // "myNumber1 is: 15" | |
cout << "myNumber2 is: " << myNumber2 << endl; // "myNumber2 is: 25" | |
return 0; | |
} | |
/* | |
* Accepts a references to an int and increments the variable by 5. | |
*/ | |
void addFiveToNum(int &n) { | |
n = n + 5; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment