Created
January 11, 2022 15:13
-
-
Save IndrajeetPatil/84417c0687ba45a339f6303010601435 to your computer and use it in GitHub Desktop.
Passing by value versus reference
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> | |
void passValue(int x) | |
{ | |
x += 2; | |
} | |
void passRef(int &x) | |
{ | |
x += 2; | |
} | |
int main() | |
{ | |
int x = 3; | |
int &y = x; | |
passValue(x); // pass by value | |
std::cout << x << std::endl; // x is still 3 | |
passRef(y); // pass by reference | |
std::cout << x << std::endl; // x is now 5! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment