Pointer Program #2

11. Swap two numbers using user defined function. (Use concept of Call by value).

#include<stdio.h>
void swap(int,int);
void main()
{
     int x,y;
     printf("Enter the values of first number:\n");
     scanf("%d",&x);
     printf("Enter the values of second number:\n");
     scanf("%d",&y);
     swap(x,y);
     printf("\nValues inside the main function:");
     printf("\nfirst=%d,second=%d\n",x,y);
}
void swap(int a,int b)
{
     int t;
     t=a;
     a=b;
     b=t;
     printf("\nValues inside the swap function:");
     printf("\nfirst=%d,second=%d",a,b);
}

Output

  • Enter the values of first number:
    3
    Enter the values of second number:
    5

    Values inside the swap function:
    first=5,second=3
    Values inside the main function:
    first=3,second=5


 

Note:

Call by value
  • In call by value, the values of actual parameters are copied to their corresponding formal parameters.
  • So the original values of the variables of calling function remain unchanged.
  • Even if a function tries to change the value of passed parameter , those changes will occur in formal parameter , not in actual parameter

For download this program please check the link ➡➡  Download


Comments

Popular

Basic Program #3

Pointer Program #1

Array Program #4

Singly Linked List program #1

Basic program #2