Sunday, August 2, 2009

C++ Value-Refrence parameters?

Does any parameter that receives a value and also sends a value outside the function have to be declared as a reference parameter?

C++ Value-Refrence parameters?
Not necessarily, you can send value from a function using function's return variable int sum(int a, int b) return a+b; will return an integer.. and also it receives values of a and b, by who ever calls the function.





Now, if you want the function to change a or b in some way and you want those changes to be visible outside of this function (sum in this example) then you will need to pass a and/or b by reference.





As the function is right now int sum(int a, int b) {...} any changes you do to the value of a or b will not be visible outside the function.





But if you declare the function as int sum(int* a, int* b) {...} now when you Change a and/or b in the function the changes will be visible outside the function and now you will have to call the function by using something like int b = sum(%26amp;a,%26amp;b). Now when
Reply:If it is being return in the parameter it is a ref...if it comes back via a return statement it does not


No comments:

Post a Comment