Briefly describe how parameter passing by value and by reference are accomplishe
ID: 3551183 • 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()
{
int bal = 12000;
//statement 1
//statement 2
}
//method A
public static void addFunds(ref int bal)
{
bal = bal + 600;
}
//method B
public static int addFunds(int bal)
{
return (bal + 600);
}
(Points : 20)
Explanation / Answer
pass by value passes the actual value to the function hence the contents of memory location are copied to function variable. Any change in function variable wont be reflected in main program
pass by refernece passes the memory location to the function hence the contents of memory location are referenced by function variable. Any change in function variable would be reflected in main program
static void
Main()
{
int bal = 12000;
addFunds(ref bal) //statement 1
bal=addFunds(bal) //statement 2
}
//method A uses pass by reference
public static void addFunds(ref int bal)
{
bal = bal + 600;
}
//method B uses pass by value
public static int addFunds(int bal)
{
return (bal + 600);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.