Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested function alternatives in C++

I want to make a function f() that uses three values to compute its result: a, b, and e. e is dependent on a and b, so technically f() is only a function of a and b. But for the sake of readability, it is easier to look at a function containing the abstraction e than to look at a messier formula containing many a's and b's.

Is there any way to use dependent variables like e without the use of nested functions, which C++ does not allow?

like image 332
john smith Avatar asked Aug 20 '26 19:08

john smith


1 Answers

C++ does have local variables, which makes this easy:

double f(double const a, double const b)
{
    double const e = a * b + b * b * b;
    return a + b + e;
}
like image 97
Ben Voigt Avatar answered Aug 23 '26 09:08

Ben Voigt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!