Briefly describe how parameter passing by-value and by-reference are accomplishe
ID: 3771941 • Letter: B
Question
Briefly describe how parameter passing by-value and by-reference are accomplished in memory. Write statement 1 to call method A below. Write statement 2 to call method B. Which method uses pass by-value? Which method uses pass by-reference?
static void Main()
{
double purchase = 1000.0;
double bonus;
//statement 1
//statement 2
}
//method A
public static double calcBonus(double purchase)
{
return (.03 * purchase);
}
//method B
public static void calcBonus(ref double bonus, ref double purchase)
{
bonus = purchase * .03;
}
Explanation / Answer
Pass By Value -: In Pass By Value you make a copy of the actual parameters in memory. Thus the memory addresses of the actual and the formal parameters are different. Hence, in Pass By Value if you make any changes in the formal parameters, those changes will not be reflected in the actual parameters.
Pass By Referrence -: In Pass By Referrence we are basically passing a pointer to the variable. A pointer is a variable that holds the address of the other variable. Suppose "A" is a actual argument whose memory address is 2000 and "a" is the formal argument which is passed by Referrence than "a" will be a pointer to "A" which means "a" will hold the address of "A". Hence if you will make any changes in the formal parameter that changes will be done at the original memory address and thus will change the actual parameter.
static void Main()
{
double purchase = 1000.0;
double bonus;
calcBonus(purchase1) // calling method A
calcBonus(&bonus, &purchase2) // calling method B by Pass By Referrence
}
//method A
public static double calcBonus(double purchase)
{
return (.03 * purchase);
}
//method B
public static void calcBonus(ref double bonus, ref double purchase)
{
bonus = purchase * .03;
}
Method A uses Pass By Value and Method B uses Pass By Referrence.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.