Skip to content

Instantly share code, notes, and snippets.

@Wollw
Created October 20, 2011 05:43
Show Gist options
  • Save Wollw/1300508 to your computer and use it in GitHub Desktop.
Save Wollw/1300508 to your computer and use it in GitHub Desktop.
C++ References Example
#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