Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

More on Functions

Prof. Debashis Hati


User Defined Function

To implement the user defined function,


there is a need of 3 elements.
1.Function declaration / prototype
2.Function definition
3.Function call
More on Functions
1.WAP to implement a function check_even_odd(int
x) to find the integer is even or odd.
1.WAP to implement a function check_even_odd(int
x) to find the integer is even or odd.
int main(){
int no;
void check_even_odd(int z);
printf(‘’Enter number”);
scanf(“%d”,&no);

return 0;
}
void check_even_odd(int x){
}
1.WAP to implement a function check_even_odd(int
x) to find the integer is even or odd.
int main(){
int no;
void check_even_odd(int z);
printf(‘’Enter number”);
scanf(“%d”,&no);
check_even_odd(no)
return 0;
}
void check_even_odd(int x){
int remainder= x%2;
if (remainnder==0)
printf(“even”);
else
printf(“odd”);
}
1.WAP to implement a function check_even_odd(int
x) to find the integer is even or odd.
int main(){
int no,x;
int check_even_odd(int z);
printf(‘’Enter number”);
scanf(“%d”,&no);
x=check_even_odd(no)
if (x==0)
printf(“even”);
else
printf(“odd”);

return 0;
}
int check_even_odd(int x){
int remainder= x%2;
if (remainnder==0)
return 0;
else
return 1;
}
1.WAP to implement a function check_even_odd(int
x) to find the integer is even or odd.
int main(){
int no,x;
int check_even_odd(int z);
printf(‘’Enter number”);
scanf(“%d”,&no);
x=check_even_odd(no)
if (x==0)
printf(“even”);
else
printf(“odd”);

return 0;
}
int check_even_odd(int x){
int remainder= x%2;
if (remainnder==0)
return 0;
else
return 1;
}
2.WAP to implement a function min(int x,int y) to
find the smallest of two integers.
3.WAP to implement a function min(int x,int y,int z)
to find the smallest of three integers.
4.WAP to implement a function min(int x,int y) to
find the smallest of four integers.
Assignment-18,16/12/21
1.WAP to implement a function void
check_even_odd(int x) to find the integer is even
or odd.
WAP to implement a function int
check_even_odd(int x) to find the integer is even
or odd.
2. int min(int,int);
3.int min(int,int,int);
4. Smallest among 4 integers using int min(int,int);
5. int power( int,int)
Parameter Passing
The mechanism to pass the information
from function call to function definition is
called parameter passing.
It is of two types
1.Call by value
2.Call by reference
Call by value
The value of arguments is copied /
transferred /passed to the formal
parameters.
int main(){ void f1(int y){
int x; y=y*2;
}
void f1(int z);

printf( “Enter the value”);


scanf(“%d”,&x);

printf(“Before function call x=%d”,x);

return 0;
}
Call by value
The value of arguments is copied /
transferred /passed to the formal
parameters.
int main(){ void f1(int y){
int x; y=y*2;
}
void f1(int z);

printf( “Enter the value”);


scanf(“%d”,&x);

printf(“Before function call x=%d”,x);

f1(x);

}
Call by value
The value of arguments is copied /
transferred /passed to the formal
parameters.
int main(){ void f1(int y){
int x; y=y*2;
}
void f1(int z);

printf( “Enter the value”);


scanf(“%d”,&x);

printf(“Before function call x=%d”,x);

f1(x);

printf(“after f unction call x=%d”,x);


return 0;
}
Call by value
The value of arguments is copied /
transferred /passed to the formal
parameters.

Whatever modifications are carried out in


formal parameters, there is no effect in
actual parameters in arguments.

You might also like