Sei sulla pagina 1di 17

EST102 Programming in C

MODULE –IV
Functions

VIVITHA VIJAY
ASSISTANT PROFESSOR
DEPARTMENT OF CSE
SRI VELLAPPALLY NATESAN COLLEGE OF
ENGINEERING
[MGIT]
Parameter(argument) passing methods
CALL BY VALUE
(OR)
PASS BY VALUE
Swapping of two numbers using call by value
#include<stdio.h>
void swap(int a,int b) → Formal parameters
{ a b
int temp;
10 20
temp=a;
a= b; 4000 4004
b=temp; a b
printf(“values inside function: a=%d, b=%d",a,b);
} 20 10
void main()
{ 4000 4004
int x,y;
printf("Enter the numbers:");
scanf("%d%d",&x,&y);
printf("Before swapping: x=%d, y=%d",x,y);
swap(x,y); → actual parameters x y
printf("After swapping : x=%d, y=%d",x,y);
10 20
}
Output
Enter the numbers:10 20 2000 2004
Before swapping: x=10 y=20
Values inside function : a=20 ,b=10
CALL BY REFERENCE
(OR)
PASS BY REFERENCE
Swapping of two numbers using call by reference
#include<stdio.h>
void swap(int *a,int *b) → Formal parameters
{ a b
int temp;
2000 2004
temp=*a;
*a=* b; 4000 4004
*b=temp; x y
printf(“values inside function:a=%d, b=%d",*a,*b);
} 20 10
void main()
{ 2000 2004
int x,y;
printf("Enter the numbers:");
scanf("%d%d",&x,&y);
printf("Before swapping: x=%d, y=%d",x,y);
swap(&x,&y); → actual parameters x y
printf("After swapping : x=%d, y=%d",x,y);
} 10 20
Output
Enter the numbers:10 20 2000 2004
Before swapping: x=10 y=20
Values inside function : a=20 ,b=10
Differences between call by value and call
by reference
Thank you

Potrebbero piacerti anche