The Wayback Machine - https://web.archive.org/web/20240118172446/https://www.geeksforgeeks.org/how-to-call-function-within-function-in-c-or-c/
Open In App
Related Articles

How to call function within function in C or C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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++ 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;
}


Output:

46

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.




// C++ program to call a function in main
#include <iostream>
using namespace std;
  
int add(int num1, int num2)
{
    return (num1 + num2);
}
  
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;
}


Output:

125

Another example of function calling function are as follows : –




#include <iostream>
using namespace std;
  
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);
}
  
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;
}


Output:

15

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 02 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads