I'm mostly interested in C++ answers for this but in addition C answers would be welcome too as they will probably vary. This is a simplified version of the real code to focus on my question so assume what I've written is correct :P
Imagine I have a function called process
in a library.
This function takes three parameters, a, b, and c
Most of the time the function only needs the values of a and b to do it's job but in some circumstances requires c.
Now I want to call this from a main program.
process(1, 2, very_expensive_call(55, 67, "Hello"));
Imagine that the value of c is very expensive to calculate. As it's probably not needed I don't want to calculate it unless I have to.
I don't want my main program to have to repeat the logic of knowing if c will be needed. I don't want the library to know about very_expensive_call
.
So how can I organize my code to prevent the call to the expensive function while remaining both efficient and elegant?