FUNCTIONS IN C
Functions
--------------
It
avoids the repeated process
A
function that calls is referred is function call and a function that is called
is referred as called function
Function
Definition
------------------------
returntype
functionname( arg1,arg2...)
{
statements;
}
Function
Declaration or Function prototype
-------------------------------------------------------
Syntax
---------
returntype
functionname(arg1,arg2...);
There
are 4 types of function
1)Function
with no argument and no return
2)Function
with no argument but return values
3)Function
with argument but no return values
4)Function
with argument and with return value
Function
with no argument and no return
----------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void name( ); --->Function prototype
void
main()
{
clrscr();
name(); ---> Function call
getch();
}
void
name() --->Function
definition
{
printf("name");
}
}
Function
with no argument but return values
----------------------------------------------------------
#include<stdio.h>
#include<conio.h>
float
pi(); ---->Function
prototype
void
main()
{
int
r;
float
area;
printf("enter
the radius");
scanf("%d",&r);
area=pi()
*r*r; ----> pi() is a
function call
printf("area
is %f",area);
getch();
}
float
pi()
{
return(3.14); ---->returns value for pi as
3.14
}
Function
with argument but no return values
----------------------------------------------------------
----------------------------------------------------------
#include<stdio.h>
#include<conio.h>
int
rev(int);
void
main()
{
int
n,r;
printf("enter
the number");
scanf("%d",&n);
r=rev(n);
getch();
}
int
rev(int n)
{
int
d,r=0;
while(n>0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
printf("the
reversed number is %d",r);
}
Function
with argument and with return value
-----------------------------------------------------------
Reversing
a number
--------------------------
#include<stdio.h>
#include<conio.h>
int
rev(int);
void
main()
{
int
n,r;
printf("enter
the number");
scanf("%d",&n);
r=rev(n);
printf("the
original number is %d",n);
printf("the
reverse is %d",r);
getch();
}
int
rev(int n)
{
int
d,r=0;
while(n>0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return(r);
}
Armstrong
number
-------------------------
#include<stdio.h>
#include<conio.h>
int
arms(int n)
void
main()
{
int
n;
printf("enter
the number");
scanf("%d",&n);
if(arms(n))
printf("%d
is an armstrong number");
else
printf("%d
is not an armstrong number");
getch();
}
int
arms(int n)
{
int
d,s=0,n1;
n1=n;
while(n>0)
{
d=n%10;
s=s+d*d*d*;
n=n/10;
}
if(s==n1)
return
1;
else
return
0;
}
Adam
number
--------------------
#include<stdio.h>
#include<conio.h>
int
adam(int);
int
rev(int)
void
main()
{
int
n;
printf("anter
a number");
scanf("%d",&n);
iif(adam(n))
printf("%d
ia an adam number");
else
printf("%d
is not an adam number");
getch();
}
int
rev(int n)
{
int
d,r=0;
while(n>0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
return
r;
}
int
adam(int n)
{
int
t,r;
t=n;
n=n*n;
r=rev(n);
n=sqrt(r);
n=rev(n);
if(n==t)
return
1;
else
return
0;
}
Swap
---------
#include<stdio.h>
#include<conio.h>
void
swap(int,int);
void
main()
{
int
a=10,b=20;
clrscr();
printf("\n
a=%d \n b=%d",a,b);
swap(a,b);
printf("\n
a=%d \n b=%d",a,b);
getch();
}
void
swap(int x,int y)f=1;
x=y;
y=t;
printf("\n
a=%d \n b=%d",x,b);
}
Factorial
-----------
#include<stdio.h>
#include<conio.h>
void
main()
{
int
n,f;
printf("enter
the value of n");
scanf("%d",&n);
f=fact(n);
printf("the
factorial is %d",f);
getch();
}
int
fact(int n)
{
int
i,f=1;
for(i=n;i>1;i--)
f=f*i;
return
f;
}
Recursive
functions
-------------------------
A
function that calls itself is called as recursive
Sum
of n numbers
------------------------
#include<stdio.h>
#include<conio.h>
int
sum(int);
void
main()
{
int
n,s;
clrscr();
printf("enter
n value");
scanf("%d",&n);
s=sum(n);
printf("the
sum is %d",s);
getch();
}
int
sum(int n)
{
int
s=0;
if(n>0)
s=n+sum(n-1);
return
s;
}
Factorial
------------
#include<stdio.h>
#include<conio.h>
void
main()
{
int
n,f;
printf("enter
the value of n");
scanf("%d",&n);
f=fact(n);
printf("the
factorial is %d",f);
getch();
}
int
fact(int n)
{
int
i,f=1;
if(n>1)
f=n*fact(n-1);
return
f;
}
No comments:
Post a Comment