Functions


What is a Function ?

Ø  A function is a self-contained block of statements that perform a particular task of some kind. Every C program can be thought of as a collection of these functions.

Ø  Any C program contains at least one function.
Ø  If a program contains only one function, it must be main( ).
Ø  If a C program contains more than one function, then one (and only one) of these functions must be main( ), because program execution always begins with main( ).

Ø  There is no limit on the number of functions that might be present in a C program.
Ø   Each function in a program is called in the sequence specified by the function calls in main( ).

Ø  After each function has done its thing, control returns to main( ).When main( ) runs out of function calls, the program ends.

Uses of  Functions

Ø  C is structural programming language, the program is divided in to small independent tasks.
Ø  These tasks are small enough toe under stand easily with out having to understand the entire program at once.

Ø   If a program is divided into functional parts then each part may be independently coded and later combined into a single unit. These sub programs called functions are much easier to under stand, debug and test. 

Advantages of Functions

            ( I ) Code Reusability
ü  Many programs require that a particular group of instructions be accessed  repeatedly from several different places with in a program.

ü  Those repeated statements  can be places wit in a single function. We use that function when ever we needs, this avoids re writing the same code this is called as “Code Reusability

( II ) Easy Debugging

ü  We write the code in smaller functions, each function contains small amount of code so it has a logical clarity.
ü  By writing functions we can easily locate the errors and we correct those very easily.



Ø  C function can be classified into two categories 1). Library functions. 2) User defined functions.

Ø  main( ) is an example of user defined function. Printf( ), scanf( ), sqrt( ), cos( ), strcat( ) belong to the category of library functions.

A function is a self-contained block of code that performs a particular task.
                    
Syntax:             
           return type   function_name (argument list……..)
           {
               local variable declarations;
                          --------- 
              stmt1;
                           stmt2;
                           ------ -
                           --------
                return (expression);
              }
.
ü  All parts are not essential. Some may be absent.  For example, the argument list and its associated argument declaration parts are optional.

Ø  The functions defined by the user based on the users requirement are called user-defined functions.  The names of the functions can also be of user interest.

In order to develop a user defined function the following parts have to be developed.
1.      Function declaration.
2.      Function  call
3.      Function definition.

Function declaration. (or) Function Prototype

Ø  A function declaration is where the name and return type of a function are given.
Ø  A function can be declared many times (as long as the declarations declare the function to be of the same type) but can only be defined once.

Ø  We can simply say that a function declaration is used to inform which function is to be used in our program and we also give the structure of that function .Structure of function means its return type and name its argument list, if we does not specify any Return value by default it returns int value.   

Syntax:-                           data type   function name (parameters);

Example :-                       double    minimum(double x, double y);
                    Int  minimum(double, double);



Function call:-
                        A function is called in the place wherever the task has to be performed.
           
Syntax:-
                     Variable = function name (parameters);

Ø  Where variable and parameters are optional based on the type of the function.        Parameters indicates the values on which the functions is to be performed. 

Ø  These parameters in the function call are called actual parameters. Variable here is used to store the value returned by the function.

   Example :-
main()
{
           minimum(1.23, 4.56);
}

Function definition:-
                This is the part where the code for the function is written.
Syntax:-
         data type function name (parameters)
         {
               local variable declaration;
               function body;
               return statement;
         }
Ø  Hence, the parameter list indicates the arguments which take values from the actual arguments of function call and are called formal parameters.

Ø  Local variables declaration indicates declaration of variables that are used in that particular function.       
Ø  Return statement is used to return a value from the function definition to the function call.

Note:-The values from actual arguments of function call are passed to formal parameters of function definition.
/* definition of minimum() */

double minimum(double x, double y)
{
double x, y;
                if (x < y)
                       return x;
               else
                      return y;
}



/* declaration of minimum() */
double minimum(double  , double  );           
main()
{            Int small,x=12.33,y=3.66;

            Small=minimum(x,y);

                printf("%f\n", small);
}
               /* definition of minimum() */
double minimum(double x, double y)
{
double x, y;
if (x < y)
return x;
else
return y;
}

No comments:

Post a Comment