Skip to study material
← Question index

QUESTION 02 · Functions

Explain user-defined functions and their advantages.

5 marks

Answer in brief

A function groups statements that perform a particular task. A program can call the function whenever that task is needed.

Main parts

  1. A declaration tells the compiler the function name, return type and parameter types.
  2. A definition contains the function body.
  3. A call requests execution of the function with the required arguments.

Example explanation

The example defines add to receive two integers and return their sum. main calls add(4, 6). The function receives the values in a and b, calculates 10 and returns it. printf then displays the returned result. The function definition appears before main, so it also provides the required declaration before the call.

Advantages

  1. Reusability: the same task can be called several times without copying its statements.
  2. Readability: a meaningful name, such as calculate_total, explains the purpose of a block.
  3. Testing: a small function is easier to check with selected inputs.
  4. Maintenance: changing one function can update all places that call it.
  5. Teamwork: different functions can be developed separately when their interfaces are agreed.

Important points

Keep each function focused on a clear task. Its parameter and return types form its interface: they describe how values enter and leave it. A function is not executed merely because it is defined; a call is needed. For the fixed integer addition shown here, time and extra working space are both O(1).

Flowchart

Control flow for function-call with labelled true and false paths.
Explain user-defined functions and their advantages. · Download diagram

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int add(int a, int b)
{
    return a + b;
}
int main(void)
{
    printf("Sum = %d\n", add(4, 6));
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

Sum = 10

Related exam questions

Related questions in the function declarations category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • March/April 2023 · Q1(h) · 1 marks

    Define function signature.

    Paper P05 · page 1
  • September 2023 · Q1(h) · 1 marks

    What is the signature of a function?

    Paper P06 · page 1

View this category in the exam question bank →