C++ Pass by Reference Program -


IBM tells C ++ pass from the reference in the example given below (in the source).

If I change from zero self I ... to zero swapnam (int i, nti j) , will it pass by value ?

// Pass Reference / Writer - Include IBM # lt; Stdio.h & gt; Zero swapnum (int and i, int and j) {int temp = i; I = J; J = temp; } Int main (zero) {int a = 10; Int b = 20; Swapanem (A, B); Printf ("A% d and B is% d \ n", a, b); Return 0; }

any Swapping If you pass by value then only are affected or appear within the function, they are not in the calling code. Also, once you return to the main you will see that A and B were not swap. This is the reason that when you want to pass swaping numbers to the referee.

If you are just asking if this will be said, then yes, you are right, you are going through the price.

Here's an example:

  #include & lt; Stdio.h & gt; Zero swapnum (int and i, int and j) {int temp = i; I = J; J = temp; } Void swapByVal (int i, int j) {int temp = i; I = J; J = temp; } Int main (zero) {int a = 10; Int b = 20; Swapanem (A, B); Printf ("swapnum is a% d and b% d \ n", a, b); SwapByVal (A, B); Printf ("swapByVal is A% d and B% d \ n", a, b); Return 0; }  

To run this code and you should see that the changes will continue through swapping only by reference, that is why we have returned back primarily to the value Are being changed. If you pass through the value, you will see that this function is calling and returning back to main fact that A and B do not really swap.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -