Functions
Functions
Functions
loops ---> code repetition --> n number of lines of same code execution
can be converted to a single loop
while(i)
{
}
................
SQUARE.c
IF we want to develop a code from a fixed place or fixed body , then go for
loop
If we want to develop a code from a place (file )and use it in another
place file , go for function using files concept
void main()
{
int a ;
int power(int ,int ); ---> function declaration
a= power(2,5 ); --> function calling //32
printf("%d",a); 32
a= power(3,4); --> function calling //81
printf("%d",a); 81
}
int power(int x ,int y )
int i ,r=1 ;
for(i=1;i<=y;i++)
{
r=r*x;
}
//r=32
return r ;
}
main
r=1
power
int x int y
3 4
i =1 r
1<=5 //
r=r*x = 1*2 =2
i =2
2<=5
r=2*2 =4
i=3
3<=5
r=4*2 =8
i=4
r=8*2= 16
i=5
r=16*2 =32
i=6
6<=5 //
int i ,r=1 ;
float f;
for(i=1;i<=y;i++)
{
r=r*x;
}
//r=32
return r;
}
void main()
{
int a ;
char
float
void
........................................
char function_name(function_arguments )
{
char c ;
return c ;
--------------------------------------------------------------------------------------------------
---------------------------------------------
float function_name(function_arguments )
{
float c ;
return c ;
--------------------------------------------------------------------------------------------------
--------------------------------------------
void
|----> void represents to not return any value from the function
--------------------------------------------------------------------------------------------------
----------------------------------------------------
void main()
{
}
......................................................................................................
void main()
{
int
void power(int ,int ); ---> function declaration
power(2,5 ); --> function calling //32
int i ,r=1 ;
for(i=1;i<=y;i++)
{
r=r*x;
}
printf("%d ",r);
}
32
81
void ---> doesn't require any return statement
......................................................................................................................
...
int i1 ,i2 ,i3 ,i4 char c1,c2,c3,c4 float f1,f2,f3,f4
declaration calling
float sum(float,float);
c1=sum(c2,i1);
void kit();
f1=kit(c1,i1,f2,f3);
..........................................................................................
1 --> one
2 --> two
45 --> forty five
Program to convert a digit to its equivalent words
.....................
22 23 24 .. 29
30 ---> develop the code inside the function
}
main()
{
long int n;
long int t ;
printf ("enter number of your choice : \n ");
scanf("%ld",&n);
if(n>=10000000)
{
t =n/10000000; --> 21
if(t<=20 )
{
txt(t);
}
else
{
txt(t/10*10);
txt(t%10);
}
txt(10000000);
n=n%10000000;
}
if(n>=100000)
{
t=n/100000; //43
if(t<=20 )
{
txt(t);
else
{
txt(t/10*10); forty
txt(t%10); three
txt(100000);
n=n/100000;
}
if(n>=1000)
{
t =n/1000; --> 65
if(t<=20 )
{
txt(t);
}
else
{
txt(t/10*10);
txt(t%10);
}
txt(1000);
n=n%1000;
}
if(n>=100)
{
t=n/100 ;
if(t<=20 )
{
txt(t); //four
}
else
{
txt(t/10*10);
txt(t%10);
}
txt(100); hundred
n=n%100; //81
printf("and ");
}
if(n> =10 )
{
t=n/10 ;
if(t<=20)
{
txt(t);
}
else
{
txt(t/10*10);
txt(t%10);
}
}
214365481 --> twenty one crore forty three lakh sixty five thousand four
hundred and eighty one ---> 9
1 2 3 4 5 6 7 8 9
56 % 10 ---> 6
156%10 --> 6
156 %100--> 56
156/100 --> 1
56%10 --> 6
56/10 --> 5
21 --> 20 + 1
t=21 %10 =1
}
else
{
txt(t/10*10);
txt(t%10);
}
}
--> this is been further converted to one more function
for suppose
void temp( long int t)
{
if(t<=20)
{
txt(t);
}
else
{
txt(t/10*10);
txt(t%10);
}
}
}
}
void temp(int t)
{
if(t<=20)
txt(t);
else
{ txt(t/10*10);
txt(t%10);
}
}
void main()
{
long int n;
int t;
printf("enter a number:\n");
scanf("%ld",&n);
if(n>=10000000)
{
t=n/10000000;
temp(t);
txt(10000000);
}
n=n%10000000;
if(n>=100000)
{
t=n/100000;
temp(t);
txt(100000);
}
n=n%100000;//67421
if(n>=1000)
{
t=n/1000;
temp(t);
txt(1000);
}
n=n%1000; //421
if(n>=100)
{
t=n/100;
if(t<=9)
txt(t);
txt(100);
}
n=n%100;
if(n>=1 &&n<=10)
{ t=n;
txt(t);
}
if(n>=10)
{
t=n;
temp(t);
}
}
......................................................................................................................
................................................................................