- 2068 Shrawan · 3+2+3 marks
Merits of functions; actual vs formal parameters
Short answer
Programmers use user-defined functions for modularity, code reuse, readability, and easier testing. Merits include maintainability and teamwork; demerits are extra call overhead and stack usage. Actual parameters appear in the call (e.g., a, b); formal parameters appear in the definition header (e.g., p, q) and receive copies of the actual values.
Why programmers prefer using user defined functions? What is merits and demerits of using functions in program? Differentiate actual and formal parameters used in functions.
Why programmers prefer user-defined functions
- They divide a large program into smaller manageable parts.
- Code written once can be reused in many places and programs.
- They shorten the program, and make it readable.
- Testing and debugging are easier, as each function is checked separately.
- They allow several programmers to work on one project.
Merits and demerits
| Merits | Demerits |
|---|---|
| Modularity and reusability | A function call takes extra time and memory (stack) |
| Shorter and clearer code | Too many functions make flow hard to follow |
| Easy maintenance and debugging | Passing many arguments is complex |
| Teamwork is possible | Needs correct prototypes, type matching and return values |
| Same logic used again and again | Recursive functions can use up the stack |
Actual versus formal parameters
| Actual parameters | Formal parameters |
|---|---|
| Values/variables written in the function call | Variables written in the function definition header |
| Belong to the calling function | Belong to the called function |
| Can be constants, variables or expressions | Must be variables with a declared type |
| Their values are sent to the function | They receive copies of the values |
#include <stdio.h>
int add(int p, int q) /* p and q are formal parameters */
{
return p + q;
}
int main(void)
{
int a = 4, b = 6;
printf("%d\n", add(a, b)); /* a and b are actual parameters */
return 0;
}
Output: 10. Here a and b are actual parameters; p and q are formal parameters.
✓ 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
- I/O functions used with data files · Computer Programming, chapter 10 · asked 2×
- Declaration vs definition; C program structure · Computer Programming, chapter 3
- Advantages of using functions · Computer Programming, chapter 6
- Reverse a string using functions · Computer Programming, chapter 7 · asked 2×
- Forward vs reverse engineering merits and demerits · Object Oriented Analysis and Design, chapter 4 · asked 3×
- Merits and demerits of active vs passive filters · Filter Design, chapter 6
- Convert user-defined type to basic type · Object Oriented Programming, chapter 5
- Three pipe materials with merits/demerits · Water Supply Engineering, chapter 8 · asked 4×