Skip to main content

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

MeritsDemerits
Modularity and reusabilityA function call takes extra time and memory (stack)
Shorter and clearer codeToo many functions make flow hard to follow
Easy maintenance and debuggingPassing many arguments is complex
Teamwork is possibleNeeds correct prototypes, type matching and return values
Same logic used again and againRecursive functions can use up the stack

Actual versus formal parameters

Actual parametersFormal parameters
Values/variables written in the function callVariables written in the function definition header
Belong to the calling functionBelong to the called function
Can be constants, variables or expressionsMust be variables with a declared type
Their values are sent to the functionThey 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)

All Computer Programming chapter 6 questions →