Here, we will pass parameters using call by value mechanism to the user-defined function. In the call by value parameter passing, the updated value of parameters does not reflect in the calling function.
In the above program, we created a function Sum() that accepts three arguments $num1, $num2, and $num3.
In the Sum() function, we calculated the addition of $num1 and $num2 and assigned the result to the $num3. Here, we printed the value of $num3 inside the function, it will print the sum of $num1 and $num2 on the webpage.
After that we created three variables $num1, $num2, and $num3 that are initialized with 10,20, and 30 respectively. Then we called Sum() method and printed the value of $num3, and it will print 0 because here we passed the parameters as call by value mechanism. And we know that in the call by value parameter passing, the updated value of parameters does not reflect in the calling function.
Program/Source Code:
The source code to demonstrate the call by value parameter passing is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created a function Sum() that accepts three arguments $num1, $num2, and $num3.
In the Sum() function, we calculated the addition of $num1 and $num2 and assigned the result to the $num3. Here, we printed the value of $num3 inside the function, it will print the sum of $num1 and $num2 on the webpage.
After that we created three variables $num1, $num2, and $num3 that are initialized with 10,20, and 30 respectively. Then we called Sum() method and printed the value of $num3, and it will print 0 because here we passed the parameters as call by value mechanism. And we know that in the call by value parameter passing, the updated value of parameters does not reflect in the calling function.
need an explanation for this answer? contact us directly to get an explanation for this answer