How to call function within function in C or C++ Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report When we begin programming in C/C++, we generally write one main() function and write all our logic inside this. This approach is fine for very small programs, but as the program size grows, this become unmanageable. So we use functions. We write code in the form of functions. The main function always acts as a driver function and calls other functions. C++ // C++ program to call a function in main #include <iostream> using namespace std; // Function called inside main int add(int num1, int num2) { return (num1 + num2); } // Driver code int main() { int num1 = 12, num2 = 34; cout << add(num1, num2); return 0; } C // C program to call a function in main #include <stdio.h> // Function called inside main int add(int num1, int num2) { return (num1 + num2); } // Driver code int main() { int num1 = 12, num2 = 34; printf("%d", add(num1, num2); return 0; } Output46 We can also write function call as a parameter to function. In the below code, first add(num1, num2) is evaluated, let the result of this be r1. The add(r1, num3) is evaluated. Let the result of this be r2. Finally add(r2, num4) is evaluated and its result is printed. CPP // C++ program to call a function in main #include <iostream> using namespace std; // Function to add two numbers int add(int num1, int num2) { return (num1 + num2); } // Driver code int main() { int num1 = 12, num2 = 34, num3 = 67, num4 = 12; // The innermost add() is computed first, then middle // add(), then the outermost add() cout << add(add(add(num1, num2), num3), num4); return 0; } C // C program to call a function in main #include <stdio.h> // Function to add two numbers int add(int num1, int num2) { return (num1 + num2); } // Driver code int main() { int num1 = 12, num2 = 34, num3 = 67, num4 = 12; // The innermost add() is computed first, then middle // add(), then the outermost add() printf("%d", add(add(add(num1, num2), num3), num4)); return 0; } Output125 Another example of function calling function are as follows : - CPP // C++ program to show how to call a function inside a // function #include <iostream> using namespace std; // funtion declartion int add(int num1, int num2); int sub(int num1, int num2); int mul(int num1, int num2); int calculator(int num1, int num2, int option) { // calling add function within calculator function if (option == 1) { return add(num1, num2); } // calling sub function within calculator function if (option == 2) { return sub(num1, num2); } // calling mul function within calculator function if (option == 3) { return mul(num1, num2); } } // function for adding two numbers int add(int num1, int num2) { return (num1 + num2); } // function for subtracting numbers int sub(int num1, int num2) { return (num1 - num2); } // function for multiplying two number int mul(int num1, int num2) { return (num1 * num2); } // driver code int main() { int num1 = 10, num2 = 5; // variable // giving options for different // calculation(add, sub, mul, div) int option; option = 1; // Assuming that user inputs 1 cout << calculator(num1, num2, option); return 0; } C // C program to show how to call a function inside a // function #include <stdio.h> // function declarations int add(int num1, int num2); int sub(int num1, int num2); int mul(int num1, int num2); int calculator(int num1, int num2, int option) { // calling add function within calculator function if (option == 1) { return add(num1, num2); } // calling sub function within calculator function if (option == 2) { return sub(num1, num2); } // calling mul function within calculator function if (option == 3) { return mul(num1, num2); } } // function for adding two numbers int add(int num1, int num2) { return (num1 + num2); } // function for subtracting numbers int sub(int num1, int num2) { return (num1 - num2); } // function for multiplying two numbers int mul(int num1, int num2) { return (num1 * num2); } // driver code int main() { int num1 = 10, num2 = 5; // variables // giving options for different // calculations (add, sub, mul, div) int option; option = 1; // Assuming that user inputs 1 printf("%d\n", calculator(num1, num2, option)); return 0; } Output15 Comment More infoAdvertise with us Next Article How to return a Pointer from a Function in C T tusharupadhyay Follow Improve Article Tags : C Language CPP-Functions Similar Reads How to call some function before main() function in C++? Since it is known that main() method is the entry point of the program. Hence it is the first method that will get executed by the compiler. But this article explains how to call some function before the main() method gets executed in C++. How to call some function before main() function? To call so 3 min read How to return a Pointer from a Function in C Pointers in C programming language is a variable which is used to store the memory address of another variable. We can pass pointers to the function as well as return pointer from a function. But it is not recommended to return the address of a local variable outside the function as it goes out of s 2 min read How to Declare a Pointer to a Function? A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming.In this article, we w 2 min read Function Pointers and Callbacks in C++ A callback is a function that is passed as an argument to another function. In C++, we cannot directly use the function name to pass them as a callback. We use function pointers to pass callbacks to other functions, store them in data structures, and invoke them later. In this article, we will discu 2 min read Methods vs. Functions in C++ with Examples A method is a procedure or function in OOPs Concepts. Whereas, a function is a group of reusable code which can be used anywhere in the program. This helps the need for writing the same code again and again. It helps programmers in writing modular codes. Methods: A method also works the same as that 3 min read C++ Function Call By Pointer Several ways exist in which data (or variables) could be sent as an argument to a function. Two of the common ones are Passing by Value and Passing by Reference. Example: C++ // C++ Program to demonstrate // Pass by value and // Pass by reference #include <iostream> using namespace std; // Pas 3 min read Like