C Program to Illustrate Pass by Value

«
»

This is a C program to illustrate pass by value.

Problem Description

The program illustrates pass by value method.

Problem Solution

1. Take two numbers as input.
2. Using a function, swap their values. You will notice that we are passing the values in the swap() function, hence the swapped values will NOT be reflected in main() function.
3. Print the output and exit.

advertisement
Program/Source Code

Here is source code of the C Program to illustrate pass by value. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Illustrate Pass by Value.
  3.  */
  4. #include <stdio.h>
  5.  
  6. void swap(int a, int b)
  7. {
  8.     int temp;
  9.     temp = a;
  10.     a = b;
  11.     b = temp;
  12. }
  13.  
  14. int main()
  15. {
  16.     int num1 = 10, num2 = 20;
  17.  
  18.     printf("Before swapping num1 = %d num2 = %d\n", num1, num2);
  19.     swap(num1, num2);
  20.     printf("After swapping num1 = %d num2 = %d \n", num1, num2);
  21.     return 0;
  22. }
Program Explanation

1. Take two numbers as input and store it in the variables num1 and num2 respectively.
2. Call the function swap and pass the variables num1 and num2 as parameters to the function swap.
3. In function swap, recieve the parameters through variables a and b respectively.
4. Copy the value of variable a to the variable temp. Copy the value of variable b to the variable a and copy the value of variable temp to the variable b. This will do the swapping ONLY in the swap() function, but it will NOT change the value of variables in the main() function.
5. Print the variables num1 and num2 in the main function as output and exit.

advertisement
advertisement
Runtime Test Cases
 
Before swapping num1 = 10 num2 = 20
After swapping num1 = 10 num2 = 20

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter