---
title: "Nested function"
subject: "Computer Programming"
chapter: 6
key: "semester_1/computer_programming:nested-function"
source: https://www.bctengineer.com/q/semester_1/computer_programming/nested-function
site: BCT Engineering
---

# Nested function

Computer Programming · Chapter 6: User-Defined Functions

- Asked in: 2071 Chaitra, 2 marks
- Video: [30b Nesting of Function   Explanation and Example Program   C Programming Language](https://www.youtube.com/watch?v=_MMq-n-rt5I) — SP Engg. ACADEMY
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/nested-function
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-6

## Question

What do you mean by nested function? Give an example.

## Short answer

Standard C does not allow function definitions inside other functions. Nesting means calling one function from within another; the called function executes and returns a value to the caller.

## Answer

A **nested function** means a function used inside another function. In standard C, a function cannot be *defined* inside another function; all functions are defined separately at the same level. Nesting is done by **calling** one function from within another function, forming a chain of function calls (nested calls). (Some compilers such as gcc allow defining a function inside another as a non-standard extension.)

```c
#include <stdio.h>
int square(int n) { return n * n; }
int sum_of_squares(int a, int b)
{
    return square(a) + square(b);   /* function called inside another function */
}
int main(void)
{
    printf("%d\n", sum_of_squares(3, 4));
    return 0;
}
```
Output:
```text
25
```

`main()` calls `sum_of_squares()`, which in turn calls `square()` twice. The result is 3² + 4² = 25.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Can a function be defined inside another?
   A. Yes
   B. Only main
   C. Only void
   D. No, not in standard C
   Answer: D. Non-standard gcc. (Key point: Standard C)

2. How is nesting done?
   A. Calling one function from another
   B. Defining
   C. Macros
   D. Goto
   Answer: A. Nested calls. (Key point: Nesting)

3. sum_of_squares(3,4)?
   A. 14
   B. 25
   C. 49
   D. 7
   Answer: B. 9+16. (Key point: Example)

## Related in Computer Programming

- [Nested structures with example](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Anested-structure-explain) — Computer Programming, chapter 8, asked 7×

## Related in other subjects

- [Nested-mass system transfer function with F-V and F-I](https://www.bctengineer.com/question.md?key=semester_3%2Fcontrol_systems%3Atf-fv-fi-m1-m2-nested) — Control Systems, chapter 2
- [Nested transactions; optimistic CC advantages](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Anested-optimistic-advantages) — Distributed System, chapter 8
- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×
- [Static data member and member function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astatic-member-function) — Object Oriented Programming, chapter 4, asked 3×
