Function in C language :

What is function ?

Functions are subprograms which are used to compute a value or perform a task. They cannot be run independently and are always called by the main() program or by other functions.
There are two king of functions :

  • Library or built-in functions (scanf(), printf(), strcpy, strcmp, strlen, strcat etc.)
  • User-defined functions

  • User-defined functions :

    In C language, functions are declared to compute and return the value of specific data type to the calling program.
    Functions can also be written to perform a task. It may return many values indirectly to the calling program and these are referred as void function (Defined by the user at the time of writing the program) .

    Uses of functions :

    function are very much useful when a block of statements has to be written/executed again and again.
    Functions are useful when the program size is too large or complex.
    Functions are also used to reduce the difficulties during debugging a program.

    Function Declaration :

    Syntax :

    1. type name ( type argu1, type argu2, ..., type argu n )
    2. {
    3. <local declaration>
    4. - - - -
    5. <statement block>
    6. - - - -
    7. return(variable or expression);
    8. }

    where type is the type of the value return by the function and argument expected.
    argu1, type argu2, ...,argu n are the arguments which are variables that will received the values from the calling program.
    name is the name of the function by which the function is called by the calling program.

    Example :

    1. /* Description: function to find factorial of an integer */
    2. int fact(int k)
    3. {
    4. int i,p = 1;
    5. for(i=1;i<=k;i++)
    6. p = p * i;
    7. return(p);
    8. }

    Computer Science Engineering

    Special Notes

    It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.

    CSE Notes