What are pass by reference and passby value?

varunM2005-12-21T22:14:01Z

Favorite Answer

classic example
Suppose you want to write a function to exchange the values of two integer variables. It seems like that would be fairly trivial. For example, to swap x and y, you could just create an extra int variable, t, and write code that looks like this:
t = x;
x = y;
y = t;
That would certainly work. (Try giving x and y initial values & trace through the code if you don't see why the code works. Note that this approach is similar to what you did for the mandel() function in that programming assignment.)

So, you would probably suppose that you could write a function like this:

void swap (int x, int y) {

int t = x;

x = y;

y = t;
}
Now, suppose your main() looks like this:

int main(void)
{
int a = 5;

int b = 7;

swap( a, b );

printf("a is %d and b is %d\n", a, b);

}

What will print? If swap works, we expect "a is 7 and b is 5" to print. But what actually happens? When the function swap is called, the values of a and b are passed into the functions. When swap begins, x is initialized to 5 and y is initialized to 7. The values of x and y are swapped...but the values of a and b (in main) are unaffected. The swap function does not work as written above because the arguments were passed 'by value.'

With pointers, we can pass the values 'by reference.' Passing by reference means that we'll pass the addresses of the variables instead of the values. Here's how swap and main should be written:

void swap ( int *px, int *py) {

int t = *px;

*px = *py;

*py = t;

}

int main(void)
{
int a = 5;

int b = 7;

swap( &a, &b );

printf("a is %d and b is %d\n", a, b);

}

Now, the addresses of the variables a and b are passed into swap. In swap, t is set to the value of 'the data that px points to', which is just the value stored at the address of a, which is 5. Then the value stored at address px is changed to the value stored at address py. This sets a to the value of b. Finally, b is set to the value t, which was the value originally stored at a. So, the values of a and b were swapped.

VJS2005-12-22T07:49:58Z

These r two method of function calling in any programming language. In Call by value we pass value which creates a duplicate value for the function but in case of call by reference we pass address of variable.

Shunmuga prasath2005-12-22T04:22:16Z

Simply pass by value is passing values alone to methods or functions while in pass by reference, address of the value is passed.

shanz24302005-12-21T23:14:50Z

o my god.i m fed up with this q. it always come in my exams.anyways i think varun's answer help u.

Anonymous2016-12-18T08:18:08Z

one way: <enter id="in1" ... /> <enter id="in2" ... /> as quickly as you click a button/link, you call a javascript function: function foo() { var in1 = checklist.getElementById("in1").value; var in2 = checklist.getElementById("in2").value; window.section = "myphpfile.very own residing homestead website?a=" + in1 + "b=" + in2; }