To study and implement Pointer Operations (Call by value and Call by reference)
Vs code
Feature | Call By Value | Call By Refernce |
---|---|---|
Definition | A function receives a copy of the argument's value, not the original data. Changes made within the function affect only the copy and do not alter the original data. |
A function receives a reference (or address) to the original argument, allowing it to modify the actual data. Changes made within the function directly affect the original variable. |
Data Passed | Copy of the argument's value. | Address (pointer) to the data. |
Data Size Efficiencys | Less efficient for large data. | More efficient for large data. |
Safety | Safer, as original data remains unchanged. | Riskier, as original data can be altered. |
when a function is called, a copy of the actual argument's value is passed to the function.
The function works with this copy, rather than the original data.
Changes made to the parameter within the function do not affect the original argument outside the function.
Safety: Protects the original data from unintended changes.
Simplicity: Easier to understand and debug, as the function operates on a copy.
Overhead: Copying large data structures can be inefficient in terms of both time and memory.
Limited Modification: Functions cannot alter the original argument, which may be limiting for certain operations.
A function receives a reference (or address) to the actual argument rather than a copy of its value.
This means that the function can directly access and modify the original data stored at that address.
This method is commonly used in languages like C++.
Performance: Efficient for large or complex data types since it avoids the cost of copying data.
Direct Modifications: Allows functions to change the argument directly, which is useful for operations like sorting, updating, or modifying objects.
Side Effects: The original data can be altered unexpectedly, which can lead to bugs or unintended behavior if not managed carefully.
Debugging Complexity: Tracking changes to data through references can be more challenging, as changes affect the original data and not just the function's local copy.
//subham
//entc B2
//23070123132
//experiment 10
//Pointer operations - call by value
#include<iostream>
using namespace std;
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
cout << "Value of a is: " << x << endl;
cout << "Value of b is: " << y << endl;
}
int main()
{
int a,b;
cout<< "Enter the value for a and b: "<<endl;
cin >> a >> b;
swap(a, b);
return 0;
}
//subham
//entc B2
//23070123132
//experiment 10
//Pointer operations - call by reference
#include<iostream>
using namespace std;
void swap(int *x, int *y) // use of pointers
{
int swap;
swap = *x;
*x = *y;
*y = swap;
}
int main()
{
int a,b;
cout<<"Enter a: "<<endl;
cin>>a;
cout<<"Enter b: "<<endl;
cin>>b;
swap(&a,&b); // Passing addresses of a and b
cout<<"Value of a is: "<<a<<endl;
cout<<"Value of b is: "<<b<<endl;
return 0;
}
I learnt about pointers and how to pass arguments to functions using call by value and call by reference methods. I also learnt how to swap values using call by reference.