How to call function within function in C or C++ Last Updated : 29 Jul, 2024 Comments Improve Suggest changes 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 call function within function in C or C++ T tusharupadhyay Follow Improve Article Tags : C++ Programs C Language C++ CPP-Functions Practice Tags : CPP Similar Reads How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read Returning a function pointer from a function in C/C++ In C/ C++, like normal data pointers(int *, char *, etc), there can be pointers to functions. Every function created in a program gets an address in memory since pointers can be used in C/C++, so a pointer to a function can also be created. Syntax: return type (*function_pointer_name) (argument_type 6 min read Function Pointer to Member Function in C++ In C++, function pointers enable users to treat functions as objects. They provide a way to pass functions as arguments to other functions. A function pointer to a member function is a pointer that points to a non-static member function of a class. In this article, we will learn how to use a functio 3 min read How to Call a Virtual Function From a Derived Class in C++? In C++, virtual functions play an important role because they allow the users to perform run-time polymorphism. While dealing with inheritance and virtual functions, it is very crucial to understand how to call a virtual function from a derived class. In this article, we will learn how to call a vir 2 min read How to Overload the Function Call Operator () in C++? In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi 2 min read How to Return a Pointer from a Function in C++? In C++, we can return a pointer from a function which is useful when we want to return large data structures that cannot be returned by value. However, it must be done carefully to avoid memory leaks, dangling pointers, and other issues related to dynamic memory management. In this article, we will 2 min read Overloading of function-call operator in C++ In this article, we will discuss the Overloading of the function-call operators in C++. The function call operator is denoted by â()â which is used to call function and pass parameters. It is overloaded by the instance of the class known as a function object.When the function call operator is overlo 3 min read How to Access a Local Variable from a Different Function in C++? In C++, local variables defined within a function in C++ are only available inside the scope of that particular function. In some situations, we may need to access these local variables in some other function. In this article, we will discuss how to access a local variable from a different function 3 min read Function Overloading vs Function Templates in C++ In C++, both function overloading and function templates allow us to create functions that can operate on different types of data. While they might seem similar, they are used for different purposes. In this article, we will learn the differences between function overloading and function templates, 4 min read Passing a Function as a Parameter in C++ Passing a function as an argument is useful in dynamically changing the behaviour of the function. This concept has already been used while passing a custom comparator function as an argument in sort() function to sort a sequence of objects as per the need.Function that is passed as argument is call 4 min read Like