Explain Call by Reference in C language ?

Answer : In call by reference, original value is modified…

Explain Call by Reference in C language ?

Call by value

  • In call by reference, original value is modified because we pass reference (address).
  • Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.

Sample Code

[pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0Avoid%20change(int%20*num)%20%0A%7B%20%20%20%20%0A%20%20%20%20printf(%22Before%20adding%20value%20inside%20function%20num%3D%25d%20%5Cn%22%2C*num)%3B%20%20%20%20%0A%20%20%20%20(*num)%20%2B%3D%20100%3B%20%20%20%20%0A%20%20%20%20printf(%22After%20adding%20value%20inside%20function%20num%3D%25d%20%5Cn%22%2C%20*num)%3B%20%20%20%20%0A%7D%20%20%20%20%20%20%0Aint%20main()%20%0A%7B%20%20%20%20%0A%20%20%20%20int%20x%3D100%3B%20%20%20%20%0A%20%20%20%20printf(%22Before%20function%20call%20x%3D%25d%20%5Cn%22%2C%20x)%3B%20%20%20%20%0A%20%20%20%20change(%26x)%3B%20%20%20%20%0A%20%20%20%20printf(%22After%20function%20call%20x%3D%25d%20%5Cn%22%2C%20x)%3B%20%20%20%20%0A%20%20%20%20return%200%3B%20%20%0A%7D%20″ message=”” highlight=”” provider=”manual”/]

Output

[pastacode lang=”c” manual=”Before%20function%20call%20x%3D100%0ABefore%20adding%20value%20inside%20function%20num%3D100%0AAfter%20adding%20value%20inside%20function%20num%3D200%0AAfter%20function%20call%20x%3D200″ message=”” highlight=”” provider=”manual”/]
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like