What is the difference between pass-by-value and pass-by-reference?

In C++, pass-by-value and pass-by-reference are two different ways to pass arguments to functions.

Pass-by-Value

When you pass a variable by value, a copy of the variable is made and passed to the function. Changes made to the parameter inside the function do not affect the original variable.

Pass-by-Reference

When you pass a variable by reference, a reference (or alias) to the original variable is passed to the function. Changes made to the parameter inside the function will affect the original variable.

Example

// Pass-by-Value void passByValue(int num) { num = num * 2; } // Pass-by-Reference void passByReference(int &num) { num = num * 2; } int main() { int x = 5; passByValue(x); // x remains 5 passByReference(x); // x now becomes 10 return 0; }

C++ pass-by-value pass-by-reference function arguments programming concepts