{"id":218,"date":"2020-07-18T09:52:14","date_gmt":"2020-07-18T09:52:14","guid":{"rendered":"\/?p=218"},"modified":"2022-04-13T05:36:19","modified_gmt":"2022-04-13T05:36:19","slug":"c-program-to-swap-two-numbers-showing-call-by-reference","status":"publish","type":"post","link":"http:\/\/suryaprakash.com.np\/?p=218","title":{"rendered":"C Program to swap two numbers showing call by reference."},"content":{"rendered":"<p><code>\/\/Application of Pointer variable<\/code><br \/>\n<code>#include&lt;stdio.h&gt;<\/code><br \/>\n<code>void swap(int *p,int *q);<\/code><br \/>\n<code>void main()<\/code><br \/>\n<code>{<\/code><br \/>\n<code>int a,b;<\/code><br \/>\n<code>printf(\"enter the value of two variable a and b\\n\");<\/code><br \/>\n<code>scanf(\"%d%d\",&amp;a,&amp;b);<\/code><br \/>\n<code>printf(\"Before Swap:\\na=%d\\tb=%d\",a,b);<\/code><br \/>\n<code>swap(&amp;a,&amp;b);\u00a0 \u00a0 \u00a0 \u00a0\/\/call by reference<\/code><br \/>\n<code>printf(\"\\nAfter Swap:\\na=%d\\tb=%d\",a,b);<\/code><br \/>\n<code>}<\/code><br \/>\n<code>void swap(int *p,int *q)\u00a0 \u00a0 <\/code><br \/>\n<code>{<\/code><br \/>\n<code>int t;<\/code><br \/>\n<code>t=*p;<\/code><br \/>\n<code>*p=*q;<\/code><br \/>\n<code>*q=t;<\/code><br \/>\n<code>}<\/code><\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-219\" src=\"\/wp-content\/uploads\/2020\/07\/pointer3-300x127.png\" alt=\"\" width=\"300\" height=\"127\" srcset=\"\/wp-content\/uploads\/2020\/07\/pointer3-300x127.png 300w, \/wp-content\/uploads\/2020\/07\/pointer3.png 457w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p style=\"text-align: justify;\">It is impossible to swap the value of two variable using function call by value.<br \/>\nBecause when an argument is passed by value, the data item is copied to the function. Thus any alteration made to the data item within the function is not carried over into the calling routine.But in call by reference, the address of the variable is passed. The content of that address can be accessed freely, either within the function or within the calling routine.Moreover any change that is made to the data item(that is to the content of the address) will be recognized in both the function and the calling routine. Thus the use of a pointer as a function argument permits the corresponding data item to be altered globally from within the function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/Application of Pointer variable #include&lt;stdio.h&gt; void swap(int *p,int *q); void main() { int a,b; printf(&#8220;enter the value of two variable &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"http:\/\/suryaprakash.com.np\/index.php?rest_route=\/wp\/v2\/posts\/218"}],"collection":[{"href":"http:\/\/suryaprakash.com.np\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/suryaprakash.com.np\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/suryaprakash.com.np\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/suryaprakash.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=218"}],"version-history":[{"count":2,"href":"http:\/\/suryaprakash.com.np\/index.php?rest_route=\/wp\/v2\/posts\/218\/revisions"}],"predecessor-version":[{"id":251,"href":"http:\/\/suryaprakash.com.np\/index.php?rest_route=\/wp\/v2\/posts\/218\/revisions\/251"}],"wp:attachment":[{"href":"http:\/\/suryaprakash.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/suryaprakash.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/suryaprakash.com.np\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}