Functions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

FUNCTIONS

loops ---> code repetition --> n number of lines of same code execution
can be converted to a single loop

Functions ---> code reusage

ABC.c --> to calculate the power of any number -


main()
{

while(i)
{

}
................
SQUARE.c

sum of SQUARE NUMBER

power --> abc.c


sum of square

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 --> garbagevalue -->43e*2 =

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 //

|----> function name


int power(int x ,int y ) -----------> function declarator
| |-------| ------------------> function arguments
|
|---> function return type

Function arguments are


created once the function execution takes place and they are dead / not
existing once the function execution is done

function declarator == function declaration

If function is developed after the main , we need to mention function


declaration statement
If function is developed before the main , its not mandatory to mention any
function declaration statements
.......................................

int power(int x ,int y )

int i ,r=1 ;
float f;
for(i=1;i<=y;i++)
{
r=r*x;
}
//r=32
return r;
}
void main()
{

int a ;

a= power(2,5); --> function calling //32


printf("%d",a); 32
a= power(3,4); --> function calling //81
printf("%d",a); 81
}
.............................................
return statement ---> return statement is going to return the value from
the function
data type of the function should match with data type of
return variable
.............................................................

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

power(3,4); --> function calling //81


81
}
void power(int x ,int y )

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

int sum(int ,int ); i1=sum(i2,i3); // example


sum(i1,i2);

float sum(float,float);

c1=sum(c2,i1);

void kit();

void abc(int , char );

f1=kit(c1,i1,f2,f3);
..........................................................................................

1 --> one
2 --> two
45 --> forty five
Program to convert a digit to its equivalent words

.....................

first we will analyze the numbers upto 10

1 2 3 4 5 6 7 8 9 --> one word

10 --> one word

11 ,12,13,14,15,16,17,18,19 ,20 --> one words


21 ---> function invoked two times
twenty one

22 23 24 .. 29
30 ---> develop the code inside the function

31 --39 --> function invoked two times


40
50
60
70
80
90
100
101 --> function invoked two times
1000
100000
10000000

int --> -32768 to 32767


#include<stdio.h>
void txt(long int n)
{
switch(n)
{
case 1 : printf("one ");
break ;
case 2 : printf("two ");
break ;
case 3 : printf("three ");
break ;
case 4 : printf("four ");
break ;
case 5 : printf(" five");
break ;
case 6 : printf(" six");
break ;
case 7 : printf(" seven");
break ;
case 8 : printf(" eight");
break ;
case 9 : printf(" nine ");
break ;
case 10 : printf(" ten");
break ;
case 11 : printf(" eleven");
break ;
case 12 : printf(" twelve");
break ;
case 13 : printf(" thirteen ");
break ;
case 14 : printf(" fourteen");
break ;
case 15 : printf(" fifteen");
break ;
case 16 : printf("sixteen ");
break ;
case 17 : printf(" seventeen");
break ;
case 18 : printf(" eighteen");
break ;
case 19 : printf(" nineteen");
break ;
case 20 : printf(" twenty ");
break ;
case 30 : printf(" thirty");
break ;
case 40 : printf("forty ");
break ;
case 50 : printf("fifty ");
break ;
case 60 : printf(" sixty");
break ;
case 70 : printf(" seventy");
break ;
case 80 : printf(" eighty");
break ;
case 90 : printf(" ninty");
break ;
case 100 : printf(" hundred");
break ;
case 1000 : printf(" thousand");
break ;
case 100000 : printf(" lakh");
break ;
case 10000000 : printf("crore ");
break ;
default : printf("enter valid input ");

}
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

214365481 /1000000 =21

21 --> 20 + 1

t=21 /10 =2 *10 =20

t=21 %10 =1

twenty one crore


214365481 % 10000000 =4365481
4365481 % 100000 =65481

4354481 /100000 = 54481


65481%1000 = 481
...............................................................
now
if(t<=20)
{
txt(t);

}
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);
}
}
}

.....................using this function , modified code


..............................................................................................
#include<stdio.h>
void txt(long int num)
{
switch(num)
{
case 1: printf("one");
break;
case 2: printf("two");
break;
case 3: printf("three");
break;
case 4: printf("four");
break;
case 5: printf("five");
break;
case 6: printf("six");
break;
case 7: printf("seven");
break;
case 8: printf("eight");
break;
case 9: printf("nine");
break;
case 10: printf("ten");
break;
case 11: printf("eleven");
break;
case 12: printf("twelve");
break;
case 13: printf("thirteen");
break;
case 14: printf("fourteen");
break;
case 15: printf("fifteen");
break;
case 16: printf("sixteen");
break;
case 17: printf("seventeen");
break;
case 18: printf("eighteen");
break;
case 19: printf("nineteen");
break;
case 20: printf("twenty");
break;
case 30: printf("thirty");
break;
case 40: printf("Forty");
break;
case 50: printf("Fifty");
break;
case 60: printf("Sixty");
break;
case 70: printf("Seventy");
break;
case 80: printf(" Eighty");
break;
case 90: printf(" Ninety");
break;
case 100: printf(" Hundred");
break;
case 1000: printf("thousand");
break;
case 100000: printf("lakh");
break;
case 10000000: printf("crore");
break;
default: printf("error");
}

}
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);
}
}

......................................................................................................................
................................................................................

You might also like