User defined functions

User defined functions:

In some situations, while designing complex programs, a user may require to perform some specific task repeatedly and there is no library function available for performing this task . Hence to solve this problem , the programmer writes his own function(s) as per requirements in the program. These functions which are defined specifically by the user to meet his requirements are called User-defined functions.
Every C program contains atleast one function . The function which is present in every C program is main() which is a user defined function. This is the function from where the program's execution being. The library functions and user defined functions are normally used in the main() function.

Every function in C consist of following components ,
(a) Function Definition
(b) Function call
(c) Function Declaration or Function Prototype  

Function definition :

A function definition contain the actual code of the function. It basically consist of two parts : Function header and the function body. A function header contain data type of the value to be returned by the function followed by the name of the function and then the formal parameter list enclosed in parentheses . The header is followed by the function body and enclosed in curly braces containing the declarations and the statements necessary to perform the intended task.
     The syntax of function definition is 

ret_type func_name(formalparameter_list)   //function header
{
local variable declaration ;     //function body
statement1;
statement2;                                   
.................
return (expression);
}

Here, function header consist of following three type :
  • ret_type specifies the data type of the value to be returned. The type of value to be returned by a function can be specified as any of the legal type in C. If a function does not return a value then its ret_type is void.
  • func_name specifies the name of the function. The rules for naming the function are the same as used for declaring valid identifiers.
  • Formalparameter_type : It contains a list of parameters separated by commas enclosed in parentheses. The list contain declarations of variables that will receive the data sent by the calling function. These parameters are local to the block in which they are declared. They are also known as formal parameters or formal arguments . If the function has no parameter i.e. if the function does not receive any data from the calling function then either the void keyword should sppear in the parentheses or be left empty. Formal parameter list takes the following form,
  data_type par1,  data_type par2, ............
Always remember :
1. Function header must not end with a semicolon.
2. No function definition is allowed within another function definition.
3. The statements in the function body can be absent, but braces are still necessary.
4. C compilers implicitly assume int to be the return type is specified by the user.
5. Declaration of parameters cannot be combined. In other words, you have to specify separate data types of each parameter even if several parameters are of the same type. For example,
int sum(int x,y)
is illegal.

Function call :

A function which is defined needs to be used somewhere in the program so that it can perform its intended task for which it is designed. In order to use it in the program, we need to call it . This is accomplished using a function call.
   A function call in a program causes the body of the called to execute. A  function call is specified by giving the function name followed by a list of actual parameters(or arguments) separated by commas and enclosed in parentheses followed by a semicolon at the end. The actual parameters identify the values that are to be sent to the called function. They can be constants, variables and a even a complicated expression.
    When the functions is called, the compiler substitutes the first argument for the first formal parameter, second argument for the second formal parameter and so on. There must be the same number of arguments in a function call as there are parameters in the function header. Moreover the data type of each of the arguments in the function call and the order in which we specify these arguments must be same with the parameter specified in the function header.
    The syntax of a function call is as follows,
   function_name(actualparameterlist);
  For instance , the factorial function that we have defined earlier can be called in different ways . All of the following called are valid.
  factorial(5);factorial(n);factorial(n-r); 
Keep in mind that the return type is not mentioned in the function call. If no argument is required then there must be an empty pair of parentheses.
When a function return a value , it can be called in expression. It can be a part of either as assignment or parameter in a function call or an output statement. They value returned by a function can be discarded if desired such as in printf();
    Some examples of using function call in an expression.
           1.result = factorial(n)
            2. binomial_coefficent  = factorial(n) /  (factorial (r) * factorial (n-r);
  3. if (factorial(n) > 32767)
     printf(" try this program using long int");
   4. sum = pow (x,i) / factorial(i);
   5. printf("factorial of %d = %d ", i, factorial(i));
   6. y= factorial(i)/ (i+2);
  7. value = factorial(4); 
Always remember : 
1. In a function call , you only need to specify the actual parameter, not its data type .
2. If actual arguments specified in the function call are less than the formal parameters then the uninitialized formal parameters will be initialize with some garbage value.
3. If actual arguments and the formal parameters are not of the same type then implicit conversion takes place if they are of compatible types, otherwise a compile time error is reported.

Function declaration:

So far we have got some idea on how to write and call functions in a program. Now the question arises where should the function definition be placed, before main() or after the main() ? Generally, the definition of main() function usually appears first in the program followed by the definitions of all other functions. Consider the following code segment,
  main(){.................... long int k;................... k=factorial(num);........................}long int factorial(int n){...............................................................}

In the above code, you can see that the call to the factorial () in main() precedes its definition. Since the functions are compiled in the order of their appearance within the program so when the main() function is compiled, and the compiler encounters the call to the factorial() function, it will have no idea what to do with it. This is because that instance, the factorial() function has not yet been defined. This will result in a compilation error. The solution to this problem is to tell the compiler before hand that the function being called will be defined later in the program. This is accomplished using function declaration. A function declaration provides the basic information that the compiler needs to check that you are using the function correctly.
The general form of function declaration is 
ret_type function_name (parameter_list);
here, parameter_list is a comma separated list of parameters of the form,
type1  formal_parameter1,  type2   formal_parameter2, ...., typen formal_parametern

Always remember:
1. The names of the formal parameter in the function declaration and the function definition header may not necessarily be the same.
2. The types of parameters in the function prototype must match the type of parameters in the function definition in number and order. If the types don't agree, a compiler warning occurs.
3. All arguments passed to the function are converted to the declared data type when the function is called.
4. Use of parameter names in the function declaration is optional. If  you write their names, scope ends with the declaration itself. Because they have no meaning to the compiler, they are practially no more than comments telling the programmers what each parameter's purpose is. For instance,
long int fact(int);
Function declaration is valid and is same as long int fact (int n);
READ MORE



No comments

Powered by Blogger.