Monday, July 27, 2009

Whats the diference b/w "call by reference" and "call by address" in cpp?

The other responders got it partially right. While it may be true that there is no physical difference between using a reference vs a pointer, there are a couple of very important differences.





The first and primary difference is that you can't pass NULL when passing by reference. Look at the following:





void fn(int %26amp;i);


void fn(int *i);





First, note that you can overload fn, since the compiler treats the reference and pointer as being different. In the second fn (the one that takes the pointer), you would typically have to check to be sure that i is not null, whereas the first fn, you can only pass a valid int to the function (ok, it is possible to juke the system and still pass a bogus value, but you really have to work at it to make it so).





This distinction is key and goes a long ways to helping to write more robust software.





A second distinction starts getting a bit more esoteric and falls along the lines of "self documenting code" in that when you pass a reference, you don't expect the called function to try to do any pointer style manipulation (e.g. treating the value you pass as part of an array). If you look at the two definitions of fn above, the second (that takes the pointer) could easily treat the pointer as the start of an array and do some work. The second fn would have to "resort to trickery" to do a similar function. So you get a certain level of documentation depending on which param it actually takes.





As an example, you might not be too surprised if the implementation of the second fn was something like:





void fn(int *i)


{


if (i == NULL)


return;


while (*i %26gt; 0) {


printf("%d\n",*i);


i++;


}





It's a natural thing to do pointer manipulation since you explicitly passed in a pointer. To do the same in the version of fn that takes a reference, you'd have to start the function like this:





void fn(int %26amp;i)


{


int *p = %26amp;i;





The rest of the function would be the same (using p as the pointer var). This is obviously more obtuse and generally should be avoided (to be honest, the first should probably be avoided as well, but that's another topic).

Whats the diference b/w "call by reference" and "call by address" in cpp?
call by value:values of the parameters will be passed to the main function.


call by reference:it will pass the address of the parameter to the main program.
Reply:Call by reference and call by address are essentially the same thing. The only difference is that with call by address, you are visibly passing a pointer to the function. With call by reference, pointers are still being passed, but it is done "behind the scenes" by the compiler so that you don't even have to realize that pointer manipulation is happening.
Reply:all function pass their argument by VALUE this means that the scope of the variables changes only within the function, but if u want a function to take arguments and change them so u must pass the by reference and passing by reference could be done by passing a pointer to the arguments or by passing the reference(address) of ur variable, so if u have a pointer to ur variable so just pass it and if u just have ur variable so u must pass it and add the "%26amp;" char before it.


for ex.:


swap(x, y) will not work because it will just change them with the function not outside them.





but this will work





swap(int *px, int *py)





{ int temp;





temp = *px;


/* contents of pointer */





*px = *py;


*py = temp;


}
Reply:None.


The 'reference' is actually a pointer to the address of the function or parameter involved.


No comments:

Post a Comment