Briefly describe how parameter passing by-value and by-reference are accomplishe
ID: 3648214 • 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
please rate - thanks
by value -- lets say the calling program has a variable a with the value 5 in it.
if you pass a, by value, the value 5 will be put in a temporary variable in the called method. any changes to that temporary variable will not be reflected in the calling program, only in the method.
by reference --lets say the calling program has a variable a with the value 5 in it.
if you pass a, by reference, the address of variable a, will be sent to the method. so any changes made to the variable in the method, are made to the actual variable.
static void Main()
{
double purchase = 1000.0;
double bonus;
bonus=calcBonus(purchase);
calcBonus(bonus,purchase);
}
//method A
public static double calcBonus(double purchase) //pass by value
{
return (.03 * purchase);
}
//method B
public static void calcBonus(ref double bonus, ref double purchase) //pass by reference
{
bonus = purchase * .03;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.