C Programming • Arrays & Functions
C Programming / Nested functions

Nested functions

Notes 3 Arrays & Functions

In C programming, the term nested functions is often used to describe a function that is called from another function. This allows a program to divide a task into smaller functions.

Notes

Nested Functions

In C programming, the term nested functions is often used to describe a function that is called from another function. This allows a program to divide a task into smaller functions.

💡 Example
main() ↓ calculateSum() ↓ returns result

Here, main() calls another function calculateSum() to perform the calculation.

📌 Technical Note

Standard C does not allow a function definition inside another function. Functions are normally defined separately and can call one another.

Why Use Functions?

Functions divide a large program into smaller and manageable parts. One function can call another function whenever a particular task needs to be performed.

💡 Example

A student result program can use one function to calculate marks and another function to display the result.

Simple Example

#include <stdio.h> int calculateSum(int a, int b) { // calculate and return the sum return a + b; } int main() { int result; // main() calls another function result = calculateSum(10, 15); printf("Sum = %d ", result); return 0; }

Expected Output

Sum = 25

How It Works

main() ↓ calculateSum(10, 15) ↓ 10 + 15 ↓ 25 returned to main() ↓ printf() displays 25

Practical Example

Problem Statement

Write a C program in which the main() function calls another function to calculate the square of a number and displays the result.

Learning Outcomes

  • Understand how one function can call another function.
  • Pass a value to a function and receive a result.
  • Divide a program into smaller functional parts.

Hint

Create a function named square() that accepts one integer and returns its square. Call this function from main().

Theory

A function can call another function to perform a specific task. The called function executes its statements and can return a value to the calling function.

Program

#include <stdio.h> int square(int n) { // return the square of the given number return n * n; } int main() { int num, result; printf("Enter a number: "); scanf("%d", &num); // call square() from main() result = square(num); printf("Square = %d ", result); return 0; }

Expected Output

Enter a number: 7

Square = 49

Note

The function square() is defined separately from main(). The main() function calls it, passes a value, and receives the returned result.

Quick Revision

Concept Remember
Function A reusable block of code for a specific task.
Function Call Used to execute another function.
Calling Function The function that calls another function.
Called Function The function that is called to perform a task.
Return Value The result sent back by the called function.

Important Exam Questions

Short Answer Questions

  1. What is meant by nested functions in C?
  2. Can a function be defined inside another function in standard C?
  3. What is a function call?
  4. What is the difference between a calling function and a called function?
  5. How can one function call another function in C?

Long Answer Questions

  1. Explain the concept of nested functions in C with a suitable example.
  2. Write a C program in which one function calls another function and explain its working.
  3. Explain why functions are defined separately in standard C.
🎥 Recommended Learning

Watch a beginner-friendly explanation of function calling and functions working together in C.

▶ Watch: Nested Functions / Function Calling in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for nested functions and function calling will be provided here.

🧠 Mind Map

Use the mind map for quick revision of calling function, called function and return value.