Call By Value in C
In this method
of calling the function values of the original variables is passed to the function. Example
#include<stdio.h> #include<conio.h> void swap (int a, int b); void main () { int a, b; clrscr (); printf ("Enter Two Numbers:"); scanf ("%d%d", &a, &b); printf ("Before Swap\n"); printf ("A=%d\nB=%d\n", a, b); swap (a, b); printf ("Before Swap\n"); printf ("A=%d\nB=%d\n", a, b); getch (); } void swap (int a, int b) { int c ; c = a ; a = b ; b = c ; } Output Enter Two Numbers: 2 3 Before Swap A = 2 B = 3 After Swap A = 2 B = 3 |
Call By Referance
In this method of calling the function addresses of the original variables is passed to the function using concept of pointer. Example #include<stdio.h> #include<conio.h> void swap (int *p, int *q); void main () { int a, b; clrscr (); printf ("Enter Two Numbers:"); scanf ("%d%d", &a, &b); printf ("Before Swap\n"); printf ("A=%d\nB=%d\n", a, b); swap (&a, &b); printf ("Before Swap\n"); printf ("A=%d\nB=%d\n", a, b); getch (); } void swap (int *p, int *q) { int c; c = *p; *p = *q; *q = c; } Output Enter Two Numbers: 2 3 Before Swap A = 2 B = 3 After Swap A = 3 B = 2 |