This is a C program to illustrate pass by value.
The program illustrates pass by value method.
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.
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.
* C Program to Illustrate Pass by Value.
void swap(int a, int b)
temp = a;
a = b;
b = temp;
int num1 = 10, num2 = 20;
printf("Before swapping num1 = %d num2 = %d\n", num1, num2);
printf("After swapping num1 = %d num2 = %d \n", num1, num2);
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.
Before swapping num1 = 10 num2 = 20 After swapping num1 = 10 num2 = 20
Sanfoundry Global Education & Learning Series – 1000 C Programs.