Call Function Within a Function in C++

  1. Function Call Mechanics Step by Step in C++
  2. Use the return Statement to Call a Function Within a Function in C++
  3. Use std::pair to Return Two Values From the Function in C++

This article will explain several methods of how to call a function within a function in C++.

Function Call Mechanics Step by Step in C++

The calling of a function entails calculating the values for its arguments, which are put in the local scope. Since there are local variables usually involved in a function body, there needs to be a new memory space that is called a stack frame. Function arguments are copied or referenced with corresponding names and converted if the cast operation is involved. Once the previous steps are done, the statements in the function block get executed until the return is encountered. The return statement forces the control flow to be returned to the calling function. At this point, the automatically allocated stack frame is discarded, and control continues from the caller code.

The functions have a return type that defines the type of object that should be passed to the caller code. We can have a function that does not return any value and is denoted with the void type. In case the function has a valid return type, we can chain the multiple function calls as demonstrated in the following example code. Note that the inner addTwoInts gets executed first, and the returned value is passed to the outer addTwoInts as one of its arguments.

#include <iostream>

using std::cout; using std::cin;
using std::endl; using std::string;

int addTwoInts(int i, int j) { return i + j; }

int main() {

    cout << "2 + 1 + -3  = " << addTwoInts(addTwoInts(2, 1), -3) << endl;
    cout << "(10 + 10) + (11 + -11) = " << addTwoInts(addTwoInts(10, 10), addTwoInts(11, -11)) << endl;
    cout << "(12 + -9) + 10) + -11) = " << addTwoInts(addTwoInts(addTwoInts(12, -9), 10),-11) << endl;

    return EXIT_SUCCESS;
}

Use the return Statement to Call a Function Within a Function in C++

Another useful method to invoke a function within a function is to utilize the return statement. Mind though, the called function should have a return value to fit this notation or not compile. Additionally, the calling function should have the same return type if the only expression after the return statement is the callee function, as demonstrated in the following example code.

#include <iostream>

using std::cout; using std::cin;
using std::endl; using std::string;

int addTwoInts(int i, int j) { return i + j; }

int multiplyAccumulate(int i, int j)
{
    return addTwoInts(i, i * j);
}

int main() {

    cout << "multiplyAccumulate(1,2) = " << multiplyAccumulate(2, 2) << endl;

    return EXIT_SUCCESS;
}

Use std::pair to Return Two Values From the Function in C++

There are scenarios when the function is suited to return several values to the caller. In such cases, one can utilize the std::pair structure to store each element in the corresponding data member and pass the value. More elements can be passed using the pointer to the array or any STL container that provides sufficient features for the given problem.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

std::pair<int, int> findMaxMin(vector<int> &arr)
{
    std::pair<int, int> ret;

    auto max = std::max_element(arr.begin(), arr.end());
    auto min = std::min_element(arr.begin(), arr.end());

    ret.first = arr.at(std::distance(arr.begin(), max));
    ret.second = arr.at(std::distance(arr.begin(), min));
    return ret;
}

int main() {
    vector<int> array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    auto ret = findMaxMin(array);
    cout << "Maximum element is " << ret.first << ", Minimum is " << ret.second << endl;

    return EXIT_SUCCESS;
}
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Function

  • Call by Reference vs Call by Value in C++
  • Use Void Functions in C++