Two reasons :
(i) Writing functions avoids rewriting the same code over and over. Suppose that there is a section of code in a program that calculates area of a triangle. If, later in the program we want to calculate the area of a different triangle we wont like to write the same instructions all over again. Instead we would prefer to jump to a ‘section of code’ that calculates area and then jump back to the place from where you left off. This section of code is nothing but a function.
(ii) Using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided in to separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code in to modular functions also makes the pro- gram easier to design and understand.
(a) Functions declaration and prototypes
Any function by default returns an int value. If we desire that a function should return a value other than an int, then it is necessary to explicitly mention so in the calling functions as well as in the called function. e.g
main ( )
{
float a,b,
printf (“\n Enter any number”);
scanf (“\% f”, &a );
b = square (a);
printf (“\n square of % f is % f”, a,b);
}
square (Float x)
{
float y;
y = x * x;
return (y);
}
the sample run of this program is
Enter any number 2.5
square of 2.5 is 6.000000
Here 6 is not a square of 2.5 this happened because any C function, by default, always returns an integer value. The following program segment illustrates how to make square ( ) capable of returning a float value.
main ( )
{
float square ( );
float a, b;
printf (“\n Enter any number “);
scanf (“%f” &a);
b = square (a);
printf (“\n square of % f is % f, “ a, b);
}
float square (float x)
{
float y; y= x *x; return ( y);
}
sample run
Enter any number 2.5
square of 2.5 is 6.2500000
(i) Writing functions avoids rewriting the same code over and over. Suppose that there is a section of code in a program that calculates area of a triangle. If, later in the program we want to calculate the area of a different triangle we wont like to write the same instructions all over again. Instead we would prefer to jump to a ‘section of code’ that calculates area and then jump back to the place from where you left off. This section of code is nothing but a function.
(ii) Using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided in to separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code in to modular functions also makes the pro- gram easier to design and understand.
(a) Functions declaration and prototypes
Any function by default returns an int value. If we desire that a function should return a value other than an int, then it is necessary to explicitly mention so in the calling functions as well as in the called function. e.g
main ( )
{
float a,b,
printf (“\n Enter any number”);
scanf (“\% f”, &a );
b = square (a);
printf (“\n square of % f is % f”, a,b);
}
square (Float x)
{
float y;
y = x * x;
return (y);
}
the sample run of this program is
Enter any number 2.5
square of 2.5 is 6.000000
Here 6 is not a square of 2.5 this happened because any C function, by default, always returns an integer value. The following program segment illustrates how to make square ( ) capable of returning a float value.
main ( )
{
float square ( );
float a, b;
printf (“\n Enter any number “);
scanf (“%f” &a);
b = square (a);
printf (“\n square of % f is % f, “ a, b);
}
float square (float x)
{
float y; y= x *x; return ( y);
}
sample run
Enter any number 2.5
square of 2.5 is 6.2500000