Functions


                  A number of statements grouped into a single logical unit are called a function. The use of function makes programming easier since repeated statements can be grouped into functions. Splitting the program into separate function make the program more readable and maintainable.
                  It is necessary to have a single function ‘main’ in every C program, along with other functions used/defined by the programmer.
                 A function definition has two principal components: the function header and body of the function. The function header is the data type of return value followed by function name and (optionally) a set of arguments separated by commas and enclosed in parenthesis. Associated type to which function accepts precedes each argument. In general terms function header statement can be written as
return_type  function_name (type1 arg1,type2 arg2,..,typen argn)
                where return_type represents the data type of the item that is returned by the function, function_name represents the name of the function, and type1,type2,…,typen represents the data type of the arguments arg1,arg2,..,argn.
Example: Following function returns the sum of two integers.
int add(int p,int q)
      {
      return p+q;          //Body of the function
      }
                 Here p and q are arguments. The arguments are called formal arguments or formal parameters, because they represent the name of the data item that is transferred into the function from the calling portion of the program. The corresponding arguments in the function call are called actual arguments or actual parameters, since they define the data items that are actually transferred.
                    A function can be invoked whenever it is needed. It can be accessed by specifying its name followed by a list of arguments enclosed in parenthesis and separated by commas.
e.g., add(5,10);
The following condition must be satisfied for function call.
  • The number of arguments in the function calls and function declaration must be same.
  • The prototype of each of the argument in the function call should be same as the corresponding parameter in the function declaration statement.
For example, the following program makes use of function that determine the sum of two integer quantities.
Function
                 The line of the function contains the function name, ‘add’ followed by formal arguments p and q, enclosed in parenthesis. The formal arguments p and q represents the data item that are transferred to the function from the calling portion of the program (i.e., add (a, b)). In addition, the formal arguments p and q are preceded by the data type int. i.e., it only accepts integers. When we execute this program the content of the variables a and b are copied to the formal arguments p and q respectively and the function returns the sum of p and q and is assigned to the variable c in the left side of the calling function.
                   A function may or may not return a value. A ‘return’ statement returns some value to the calling function and it may be assigned to the variable in the left side of the calling function. The return value can be a constant, variable, a user defined data structure, a general expression, a pointer to function, or a function call. The return statement also causes the program logic to return to the point from which the function was accessed. In general term the return statement is written as
return expression;
                  If a function does not return a value, the return type in the function definition and declaration is specified as void. The declaration for a prototype for a function that receive any arguments from the calling function and does not return any value will have the format
                                                void function_name (void);
The above program can be rewrite as
#include <stdio.h>
#include <conio.h>
int add(int p,int q);                       // function prototype
void display(int p, int q, int r); // function prototype
int main()
        {
        int a,b,c;
        clrscr();
        printf("Enter two numbers\n");
        scanf("%d%d",&a,&b);
        c=add(a,b);
        display(a,b,c);
        getch();
        return 0;
       }

int add(int p,int q)          //This function returns sum of a and b.
        {
        return(p+q);
        }
void display(int p, int q, int r). //This function returns nothing
        {
        printf("Sum of %d and %d is %d",p,q,r);
        }

Pass by value

                Arguments can be passed to a function by two methods, They are
  1. pass by value
  2. pass by reference
                 Function in C passes all arguments by value. When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function. Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of an argument to a function is known as passing by value.
Example:
Pass by Value

Pass by Reference

                When passing by reference technique is used, the address of the data item is passed to the called function. Using & operator we can determine the address of the data item. Note that function once receives a data item by reference, it acts on data item and the changes made to the data item also reflects on the calling function. Here you don't need to return anything to calling function.
Example:
Pass by Reference

Recursion

             Recursive functions are those functions, which call itself within that function. A recursive function must have the following type of statements.
  • A statement to test and determine whether the function is calling itself again.
  • A statement that calls the function itself and must be argument.
  • A conditional statement (if-else)
  • A return statement.
Example: Factorial of a number
             This is the most famous program on recursion. Many versions of this program are available. All programs differ only in checking conditions. I prefer to write like the following one.
Recursion

No comments: