- 2073 Shrawan · 5 marks
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 function | User-defined function |
|---|---|
| Already written and stored in the C library | Written by the programmer |
| Header file must be included | Defined/declared in the program itself |
| Cannot be modified | Can 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)
Related questions
- Significance of user defined functions · Computer Programming, chapter 6
- Switch versus user defined function · Computer Programming, chapter 5
- FORTRAN constants, variables, library functions · Computer Programming, chapter 11
- Merits of functions; actual vs formal parameters · Computer Programming, chapter 6
- Convert user-defined type to basic type · Object Oriented Programming, chapter 5
- User-level versus kernel-level threads · Embedded System, chapter 6 · asked 2×