{"id":1371,"date":"2018-01-18T13:56:23","date_gmt":"2018-01-18T13:56:23","guid":{"rendered":"http:\/\/codoplex.com\/?p=1371"},"modified":"2026-01-05T12:35:42","modified_gmt":"2026-01-05T12:35:42","slug":"function-call-value-call-reference-c-programming-code-example","status":"publish","type":"post","link":"https:\/\/blog.codoplex.com\/function-call-value-call-reference-c-programming-code-example\/","title":{"rendered":"Function Call by Value and Call by Reference in C Programming"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>In this simple C code example we tried to show the difference between function call by value and call by reference in C programming. In function call by value the original variables are not affected because the values are copied inside function definitions. Whereas in function call by reference the original variables are affected because we pass the memory addresses inside function definitions.<\/p><p>[js]<\/p><p>#include &lt;stdio.h&gt;<\/p><p>void swapCallByValue(int x1, int y1); \/\/ this is prototype of our function (call by value)<br>\nvoid swapCallByReference(int *x1, int *y1); \/\/ this is prototype of our function (call by reference)<\/p><p>int main(void){<br>\n   \/\/ in call by value the original variables are not affected because the values are copied inside functions<br>\n   \/\/ in call by reference the original variables are affected becuase we pass the memory address inside functions<\/p><p>   int number1, number2;<\/p><p>   printf(\"Please Enter First Numbern\");<br>\n   scanf(\"%d\", &amp;number1);<\/p><p>   printf(\"Please Enter Second Numbern\");<br>\n   scanf(\"%d\", &amp;number2);<\/p><p>   printf(\"Following is the result of Call By Value. You will notice that values of original variables n are not affected when passed to the swapCallByValue functionn\");<\/p><p>   printf(\"number1 variable&rsquo;s value before passing in swapCallByValue function: %dn\", number1);<br>\n   printf(\"number2 variable&rsquo;s value before passing in swapCallByValue function: %dn\", number2);<\/p><p>   \/\/ function call (call by value)<br>\n   swapCallByValue( number1 , number2);<\/p><p>   printf(\"number1 variable&rsquo;s value after passing in swapCallByValue function: %dn\", number1);<br>\n   printf(\"number2 variable&rsquo;s value after passing in swapCallByValue function: %dn\", number2);<\/p><p>   printf(\"Following is the result of Call By Reference. You will notice that values of original variables n are affected when passed to the swapCallByReference functionn\");<\/p><p>   printf(\"number1 variable&rsquo;s value before passing in swapCallByValue function: %dn\", number1);<br>\n   printf(\"number2 variable&rsquo;s value before passing in swapCallByValue function: %dn\", number2);<\/p><p>   \/\/ function call (call by reference)<br>\n   swapCallByReference( &amp;number1 , &amp;number2);<\/p><p>   printf(\"number1 variable&rsquo;s value after passing in sumCallByValue function: %dn\", number1);<br>\n   printf(\"number2 variable&rsquo;s value after passing in sumCallByValue function: %dn\", number2);<\/p><p>   return 0;<br>\n}<\/p><p>\/\/ following is the definition of our sumCallByValue function (call by value)<br>\nvoid swapCallByValue(int x1, int y1){<br>\n   int temp;<br>\n   temp = x1;<br>\n   x1 = y1;<br>\n   y1 = temp;<br>\n}<\/p><p>\/\/ following is the definition of our sumCallByReference function (call by reference)<br>\nvoid swapCallByReference(int *x2, int *y2){<br>\n   int temp;<br>\n   temp = *x2;<br>\n   *x2 = *y2;<br>\n   *y2 = temp;<br>\n}<\/p><p>[\/js]<\/p><p><strong>Output:<\/strong><\/p><p><a href=\"https:\/\/blog.codoplex.com\/wp-content\/uploads\/2018\/01\/function-call-by-value-and-call-by-reference-in-c-programming.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1372\" src=\"https:\/\/blog.codoplex.com\/wp-content\/uploads\/2018\/01\/function-call-by-value-and-call-by-reference-in-c-programming.png\" alt=\"\" width=\"775\" height=\"362\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this simple C code example we tried to show the difference between function call by value and call by reference in C programming. In function call by value the original variables are not affected because the values are copied inside function definitions. Whereas in function call by reference the original variables are affected because [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10],"class_list":["post-1371","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c-programming"],"_links":{"self":[{"href":"https:\/\/blog.codoplex.com\/wp-json\/wp\/v2\/posts\/1371","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.codoplex.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.codoplex.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.codoplex.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.codoplex.com\/wp-json\/wp\/v2\/comments?post=1371"}],"version-history":[{"count":3,"href":"https:\/\/blog.codoplex.com\/wp-json\/wp\/v2\/posts\/1371\/revisions"}],"predecessor-version":[{"id":7529,"href":"https:\/\/blog.codoplex.com\/wp-json\/wp\/v2\/posts\/1371\/revisions\/7529"}],"wp:attachment":[{"href":"https:\/\/blog.codoplex.com\/wp-json\/wp\/v2\/media?parent=1371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.codoplex.com\/wp-json\/wp\/v2\/categories?post=1371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.codoplex.com\/wp-json\/wp\/v2\/tags?post=1371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}