C function advantages



Advantages of Writing Function in C Programming

1. Modular and Structural Programming can be done

  1. We can divide c program in smaller modules.
  2. We can call module whenever require. e.g suppose we have written calculator program then we can write 4 modules (i.e add,sub,multiply,divide)
  3. Modular programming makes C program more readable.
  4. Modules once created , can be re-used in other programs.

2. It follows Top-Down Execution approach , So main can be kept very small.

  1. Every C program starts from main function.
  2. Every function is called directly or indirectly through main.[See : How to call a function (Simple Example) ]
  3. Example : Top down approach. (functions are executed from top to bottom)
main()
{
display();
}

void mumbai()
{
printf("In mumbai");
}

void pune()
{
india();
}

void display()
{
pune();
}

void india()
{
mumbai();
}

Now in the above program we have written functions in the following order –

  1. main()
  2. mumbai()
  3. pune()
  4. display()
  5. india()

We have written functions in the above specified sequence , however functions are called in which order we call them. Here functions are called this sequence

  1. main()
  2. display()
  3. pune()
  4. india()
  5. mumbai().

3. Individual functions can be easily built,tested

  1. As we have developed C application in modules we can test each and every module.
  2. Unit testing is possible.
  3. Writing code in function will enhance application development process.

4. Program development become easy

5. Frequently used functions can be put together in the customized library

  1. We can put frequently used functions in our custom header file. [ See : How to create own header file ?  ]
  2. After creating header file we can re use header file. We can include header file in other program.

6. A function can call other functions & also itself

  1. Function can call other function.
  2. Function can call itself , which is called as “recursive” function.
  3. Recursive functions are also useful in order to write system functions.

7. It is easier to understand the Program topic

  1. We can get overall idea of the project just by reviewing function names.