Make your own free website on Tripod.com

Lesson 7: User-Defined Functions.

Prev || Home || Next

Bar_line.gif (11170 bytes)

   C functions can be classified into two categories namely Library functions and User defined functions. main() is an example of user defined function, whereas printf & scanf belong to the category of library. main() is specially recognised function in C. Library functions come along with the compiler & are present on disk. Every program must have a main function to indicate where the program has to begin its execution. Though it is possible to code any program utilizing only main function, it leads to a number of problems. The program may become too large & complex & as a result the task of debugging & testing becomes difficult. If a program is divided into functional parts, then each part may be independently coded & later combined into a single unit. These subprograms called functions are much easier to understand, debug and test.

    Features:-

    1) It facilitates top down modular programming. In this style the high level logic is solved first while           details of each lower level function are addressed later.

    2) The length of a source program can be reduced.

    3) Debugging & testing becomes easy.

    4) A function may be used by many other programs.

    5) There can be any number of functions in a program, and any function can call any other function           any number of times.

    6) The order in which the functions get called & the order in which they are defined in a program need not be the same.

    example:- passing values between functions.

    main()

    {

        int a,b,c,sum;

        printf("Enter any three numbers");

        scanf("%d%d%d",&a,&b,&c);

        sum=calsum(a,b,c);

        printf("Sum = %d",sum);

    }

    calsum(x,y,z)

    int x,y,z

    {

        int s;

        s=x+y+z;

        return(s);

    }

   Note the following points carefully:-   

    1) There is no restriction on the number of return statements that may be present in a function. Also           the return statement need bot always be present at the end of the called function.

    2) A function gets called if the function name is followed by semicolon.

    3) If the function name is not followed by a semicolon it gets defined.

    4) Any C function by default returns an int value. If we desire that a function should return a non integer value, then it is necessary to mention so in the calling function as well as called function .

main()                                         main()                                         main()

{                                                {                                                 {

    int a,b,c,p;                                   float a,b,c,p;                               float a,b,c;

    a=b=c=5;                                    float prod();                                void prod();     

    p=prod(a,b,c);                             a=b=c=1.5;                                 a=b=c=1.5;

    printf("p=%d",p);                        p=prod(a,b,c);                             prod(a,b,c);

}                                                     printf("p=%d",p);                      }

prod(x,y,z)                                   }                                                  void prod(x,y,z)

int x,y,z                                         float prod(x,y,z)                          float x,y,z;

{                                                   float x,y,z;                                   {                  

    int p;                                         {                                                     float p;

    p=x*y*z;                                         float p;                                      p=x*y*z;

    return(p);                                         p=x*y*z;                                   printf("p=%d",p);

}                                                         return(p);                                }

                                                    }    

Note:-                                             Note:-                                          Note:-

1) An integer value being                 1) A float is being returned.          1) No value is being returned by

   returned by function prod().       2) For returning a float value             prod().

2) No special provision is to                the declatration in main()          2) To ensure that no value is

   be made for returning an                  as well as while defining                 returned, void declaration is

   int value.                                         the function is necessary.               made in main() and while

                                                                                                             defining the function.

Recursion:

      When some statements in a function calls the same function it is in, it is called as recursion. Such a function is called recursion function.

    Program:- To see working of recursion.

    main()

    {

        int a,fact;

        printf("Enter any number");

        scanf("%d",&a);

        fact=rec(a);

        printf("Factorial value=%d",fact);

    }

    rec(x)

    int x;

    {

        int f;

        if(x==1)

        return(1);

        else

        f=x*rec(x-1);

        return(f);

    }

Bar_line.gif (11170 bytes)

 

Prev || Home || Next