Function in c has parameter but not returning any value

void swap(int,int);
void main()
{
int a=10,b=15;
clrscr();
printf("Befor swap a=%d ,b=%d",a,b);
swap(a,b);
printf("\nAfter swap a=%d ,b=%d",a,b);
getch();
}
void swap(int a,int b)
{
int c;
c=a;
a=b;
b=c;
}
Output:
Before swap a=10,b=15
After swap a=10,b=20
Explanation: a and b are auto variable (default storage class is auto) .Here its scope is only within main function.After the main function both a and b will die.We are swapping outside the main function so value of a and b is not changing.
Post a Comment