1
41kviews
What are the different ways for parameter passing to a function? Explain along with example.
1 Answer
written 8.3 years ago by |
Passing Parameter to a Function:
Call by Value:
Program:
#include <stdio.h>
#include <conio.h>
int main() {
int a, b, ans;
int sum(int x, int y);
printf("Enter Two Values:);
scanf("%d%d", &a, &b)
ans=sum(a,b)
printf("sum=%d",ans);
getch();
}
int sum (int x, int y) {
int z;
z = x+y;
return(z);
}
Output:
Enter Two Values: 10 20
Sum = 30
Call by Reference:
Program:
#include <stdio.h>
#include <conio.h>
int main() {
int i, j;
int swap(int*x, int*y);
i=10;
j=20;
swap(&i, &j);
printf("%d%d", i, j);
getch();
}
int swap (int*x, int*y) {
int temp;
temp = *y;
*y = *x;
*x = temp;
}
Output:
i=20
j=10