C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Functions in C

Nested function calls example

C Program: Nested function calls example

C

#include <stdio.h>

 

// Function to add two numbers

int add(int a, int b) {

    return a + b;

}

 

// Function to multiply two numbers

int multiply(int a, int b) {

    return a * b;

}

 

// Function to calculate (a + b) * (c + d)

int calculate(int a, int b, int c, int d) {

    return multiply(add(a, b), add(c, d));

}

 

int main() {

    int a = 2, b = 3, c = 4, d = 5;

    int result;

 

    // Nested function calls: multiply(add(a,b), add(c,d))

    result = calculate(a, b, c, d);

 

    printf("Result = (a + b) * (c + d) = %d\n", result);

 

    return 0;

}

Output

 
OUTPUT 1 :

Result = (a + b) * (c + d) = 45

Explanation

  • The program defines three functions:
    1. add() → returns the sum of two numbers.
    2. multiply() → returns the product of two numbers.
    3. calculate() → performs a nested function call:

                multiply(add(a, b), add(c, d))

                Here, both add() functions are called first, and their results are passed to multiply().