0% found this document useful (0 votes)
4 views1 page

Pass by Value Vs Ref

Pass by Value involves passing a copy of the argument to a function, meaning changes do not affect the original variable, and is typically used for primitive data types. In contrast, Pass by Reference passes the address of the argument, allowing the function to modify the original variable, and is commonly used for larger data structures. Both methods have distinct use cases and effects on variable manipulation.

Uploaded by

oo.213h2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Pass by Value Vs Ref

Pass by Value involves passing a copy of the argument to a function, meaning changes do not affect the original variable, and is typically used for primitive data types. In contrast, Pass by Reference passes the address of the argument, allowing the function to modify the original variable, and is commonly used for larger data structures. Both methods have distinct use cases and effects on variable manipulation.

Uploaded by

oo.213h2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Pass by Value vs.

Pass by Reference in OOP

Pass by Value:

 Definition: When a function is called, a copy of the actual argument is passed to the
function.
 Effect: Changes made to the parameter inside the function do not affect the original
argument.
 Use Case: Primarily used for primitive data types like int, float, etc.
 Example:
 void function(int x) {
 x = 10; // Changes only local copy
 }
 int a = 5;
 function(a);
 // 'a' remains 5 after function call
Pass by Reference:

 Definition: When a function is called, a reference (or address) to the actual argument is
passed, allowing the function to modify the original variable.
 Effect: Changes made to the parameter inside the function will affect the original
argument.
 Use Case: Commonly used for larger data structures (like arrays or objects) to avoid
copying, and to modify the original argument.
 Example:
 void function(int &x) {
 x = 10; // Modifies the original variable
 }
 int a = 5;
 function(a);
 // 'a' becomes 10 after function call

Summary:

 Pass by Value: Creates a copy of the argument; no effect on the original variable.
 Pass by Reference: Passes the address of the argument; changes affect the original
variable.

You might also like