Skip to main content

Advantages of using functions

Short answer

Functions promote modularity, reusability, and easier debugging. Library functions (e.g., printf, sqrt) are pre-written in headers and cannot be modified; user-defined functions (e.g., cube, area) are written by the programmer, can be modified, and are defined in the program itself.

What are the advantages of using functions? Differentiate between Library functions and User-defined functions with suitable example.

Advantages of functions

  • Modularity: a big program is divided into small parts, each easy to write and test.
  • Reusability: write once, call many times (even from other programs).
  • Less code: repeated code is not rewritten, so programs become shorter.
  • Easy debugging and maintenance: an error is found and corrected in one place.
  • Team work: different programmers can develop different functions.
  • Readability: main() reads as a list of meaningful steps.

Library functions versus user-defined functions

Library functionUser-defined function
Already written and stored in the C libraryWritten by the programmer
Header file must be includedDefined/declared in the program itself
Cannot be modifiedCan be modified as required
Examples: printf(), sqrt(), strlen()Examples: cube(), area()
#include <stdio.h>
#include <math.h>
double cube(double x)          /* user-defined function */
{
    return x * x * x;
}
int main(void)
{
    printf("sqrt(25) = %.1f\n", sqrt(25.0));   /* library function */
    printf("cube(3)  = %.1f\n", cube(3.0));
    return 0;
}

Output:

sqrt(25) = 5.0
cube(3)  = 27.0

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 6 questions →