Saturday, May 9, 2009

Call by value || Call by reference for C++?

What is Call-by-reference semantics and Call-by-value semantics (in C++), and what's the difference between them.

Call by value || Call by reference for C++?
Call by value passes a parameter to the subroutine but if the subroutine modifies that parameter, it does not change the variable that was passed.


Call by reference passes the address of the actual parameter owned by the caller. If the called subroutine modifies that parameter, the calling routine will see the value of the parameter that was passed is changed.
Reply:if you want to modify a variable in a function then you should call that function the arguments of that function should be address of a variable that is called call by reference ( means that address).


void negative(int *np){*np=-(*np);}





If you call that function by value. there is no changes in that variable.


void negative(int np){np=-np;}





Try this with your c editor.

redbud

No comments:

Post a Comment