C Program to Illustrate Pass by Value

This C Program illustrates pass by value. This program is used to explain how pass by value function works. Pass by Value: In this method, the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In pass by value, the changes made to formal arguments in the called function have no effect on the values of actual arguments in the calling function.

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>
  6. void swap(int a, int b)
  7. {
  8.     int temp;
  9.     temp = a;
  10.     a = b;
  11.     b = temp;
  12. }
  14. int main()
  15. {
  16.     int num1 = 10, num2 = 20;
  18.     printf("Before swapping num1 = %d num2 = %d\n", num1, num2);
  19.     swap(num1, num2);
  20.     printf("After swapping num1 = %d num2 = %d \n", num2, num1);
  21.     return 0;
  22. }

$ cc pgm43.c
$ a.out
Before swapping num1 = 10 num2 = 20
After swapping num1 = 20 num2 = 10

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

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.
Subscribe Newsletter & Posts

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and 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 & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

Practice Interview Questions & Answers, Quizzes, Objective, Multiple Choice Aptitude Tests for Freshers and Experienced People in these topics - Learn C, C++, Java and C# Programming with coding example on Simple problems as well as tough Algorithms and Data-structures along with runtime output: - C Programs, Data-Structures and Algorithms, C++ Algorithms, Java Algorithms, C# Programs, Android Programs in Java

C Tutorials and Linux Commands Tutorial - C Tutorials, Linux Commands

Career Mentoring with our Founder / CTO - Read More