Explain the concept of pass by reference. How it differs from pass by value
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
Explain the concept of pass by reference. How it differs from pass by value
Pass by reference :It means to pass the reference of an argument in the calling function to the corresponding formal parameter of the calling function.
The called function can modify the value of the argument by using its reference passed in.
Example:
void swapNums(int &x, int &y) {
int z = x;
x = y;
y = z;
}
int main() {
int firstNum = 10;
int secondNum = 20;
cout << "Before swap: " << "\n";
cout << firstNum << secondNum << "\n";
// Call the function, which will change the values of firstNum and secondNum
swapNums(firstNum, secondNum);
cout << "After swap: " << "\n";
cout << firstNum << secondNum << "\n";
return 0;
}
Pass by reference is differ from pass by value as in pass by value a copy of the actual parameter's value is made in memory.
i.e. the caller and callee have two independent variables with the same value.
If the callee modifies the parameter value, the effect is not visible to the caller.
Now use of pass by value if we are building multi-threaded application, then we don’t have to worry of objects getting modified by other threads.
In distributed application pass by value can save the over network overhead to keep the objects in sync.
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
By default, C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function
Step by step
Solved in 3 steps