Friday 30 March 2012

Introduction - User Defined Function


User Defined Function :
         
è      Users can also define and write small program as function to do a task relevant to their programs such function are called user defined functions. main() as the specially recognized user define function in C.
è      To use the user define function in program need to establish following three elements:
1.                                                                                  Function definition
2.                                                                                  Function call
3.                                                                                  Function declaration.
è    Function definition is an independent program module that is specially written to implement the requirements of the function.
è    To access the function need to call it. It can be called at the where it needed. It is known as function call. The program that calls the function is referred to as the calling program or calling function.
è    The declaration specifies the prototype of function including the declaration of variable. The calling program should declare any function that is to be used  later in the program.


Function Definition
è    The function definition or function implementation shall includes following elements:
1.                                                                                   function name.
2.                                                                                   function type or return type
3.                                                                                   parameter list
4.                                                                                   local variable list
5.                                                                                   function statement
6.                                                                                   return statement

The general format of function definition is as follow
return_type function_name (parameter list)
parameter declaration;
{
          Local variable declarations;
          Executable statement 1;
          Executable statement 2;
          ------------------
          ------------------
          return (expression);
}
According to the above syntax the main elements of the function definition is grouped into two parts
          1. Function header
          2. Function body
Function header

è      Return type: Return type specifies the type of returned value by function if the function       does not return a value the type is defined as void.
è      Function_name : The function name is any legal identifier followed by the parentheses    without any space in between.
è      Parameter listThe parameters are given inside the parentheses preceded by their data types & separated by commas. They serves as input data to the function to carry out the specified task. The parameter may declared inside the parentheses or first list them and declare them out side of parentheses before starting the body of the function.

Function Body
è    The function body contains the declaration and statements necessary for performing the required task.
è    The body enclosed in braces, contains three parts as following
1. Local declarations that specify the variables needed by function.
2. Executable statements that performs the task of function.
3. A return statement that returns the value evaluated by function. If function does not return any value omit the return statement and must specify the return type as void.
     Ex
          void drawline()
           {
                   Printf(“\n---------------------------“);
           }
Function call

è    A user define function is invoked or called from the main program or any other function simply by using the name of the function including the parentheses followed by a semi colon.
è    It contains the list of actual parameter in parentheses.
Ex
          void main()
           {
                   printf(“\n  SMT R.O.Patel Women’s colege :”);
                   drawline();                     // Function call
                   printf(“\n Morbi :”);
                   drawine();                      // Function call
           }

// function definition      
void drawline()
           {
                   printf(“\n ---------------------------------------“);
           }

          As shown in above example here the function drawline() print the line on the screen & calling from main function.

Function declaration
Like variables, all functions in a c program must be declared, before they invoked. It is also called function prototype. They are coded in following format.
                   return_type function_name(parameter list);
It is very similar to the function header line except the terminationg semicolon
Ex,
void drawline();                                                              // Declaration
void main()                                                                    
           {
                   printf(“\n  SMT R.O.Patel Women’s colege :”);
                   drawline();                                                            // calling
                   printf(“\n Morbi :”);
                   drawine();                                                             // calling
           }

 void drawline()                                                              // Definition

           {
                   printf(“\n ---------------------------------------“);
           }

Posted By : Ruchita Pandya

No comments:

Post a Comment