Skip to content

Instantly share code, notes, and snippets.

@IndrajeetPatil
Created January 11, 2022 15:13
Show Gist options
  • Save IndrajeetPatil/84417c0687ba45a339f6303010601435 to your computer and use it in GitHub Desktop.
Save IndrajeetPatil/84417c0687ba45a339f6303010601435 to your computer and use it in GitHub Desktop.
Passing by value versus reference
#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