Find the difference between two types of returning values. Implement the followi
ID: 3679802 • Letter: F
Question
Find the difference between two types of returning values.
Implement the following function prototypes (which take hours of work as integer and the rate of pay as float and calculates the net-payment as float) on paper. Also, write a main function which calls both functions. Compare the calculated net values and show the proper message whether or not they match. Name your variables as hours, rate, and netPay.
float calNetPay(int, float);
void calNetPay2(float&, int, float);
Also, answer the following questions:
What does the operator & do? Create a table and put all of the variables of the program inside the first column. The second column should show the scope of each variable. For example for calNetpay2 function if you called the integer variables hours, its scope will be the function calNetPay2.
Explanation / Answer
Here, there is two concepts involve call by value and call by reference.
The following declaration of function is call by value:
float calNetPay(int, float);
Here, only the value of two variable (hours of work and rate of pay is passed) and resultant value i.e. net pay is returned. Hence, float is the returned type of the function.
The following declaration of function is call by reference:
void calNetPay2(float&, int, float);
Here first argument is net_pay which is passed by reference. pass by reference means reference of variable is passed to the function and if we change the value of reference variable then it will be reflect in main function. Hence, in this function we changed the value of variable net_pay which is reflected in the main function. Hence, we don't need to return the value of net_pay unlike in float calNetPay(int, float). Just remember in pass by value only the value of parameter is passed. Hence, changes to the variable will not reflect in the main function. But , in pass by reference the reference of the parameter is passed. Hence, changes in the function will reflect in the main function. You will understand by this example:
& is used to create a reference variable. reference variable stores the reference of other variable. reference is similar to the address of variable. We can access the variable by using its reference. for example in the below code: variable net_payment is a reference variable. It stores the reference of n_p variable in the main funciton.
Sample Code:
#include <iostream>
#include <cstdlib>
#include<string>
using namespace std;
float calNetPay(int hours_of_work, float rate_of_pay) // pass by value
{
return hours_of_work * rate_of_pay;
}
void calNetPay2(float& net_payment,int hours_of_work, float rate_of_pay) // pass by reference
{
net_payment = hours_of_work * rate_of_pay;
}
int main ()
{
int h_o_w;
float r_o_p, n_p; // i used shortforms here h_o_w is hours_of_work , r_o_p is rate_of_pay and n_p is net_payment
cout<<"enter hours of work and rate of pay"<<endl;
cin>> h_o_w >> r_o_p ;
cout<<" pass by value call: "<< calNetPay(h_o_w , r_o_p)<<endl;
calNetPay2(n_p,h_o_w , r_o_p);
cout<<" pass by reference call: "<<n_p<<endl;
return 0;
}
Sample Input and Output:
enter hours of work and rate of pay
2
3
pass by value call: 6
pass by reference call: 6
Table: 1) float calNetPay(int, float);
h_o_w ,r_o_p and n_p is defined in the main function . Hence it is local variable inside main . we can't access this variable outside main. The scope of local variable is inside the function only. similarly, hours_of_work and rate_of_pay are the local variable inside the calNetPay function. We can't access this variable in the main function.
2) void calNetPay2(float&, int, float)
Variable Name Scope 1) hours_of_workcalNetPay 2) rate_of_pay calNetPay 3) h_o_w main() 4) r_o_p main() 5) n_p main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.