Home > Mobile >  Calling function multiple times
Calling function multiple times

Time:08-10

I have a question. In this conditional the function is beening called every time I write the name or once it was called for the first time the value gets stored?

if(SUMAS(MADERAS, N, M, L, K) <= SUMAS(MADERAS, M, N, L, K) and SUMAS(MADERAS, N, M, L, K) != -1) 

                if(SUMAS(MADERAS, N, M, L, K)==-1)
                    cout << "impossivel" << endl;
                else
                    cout << SUMAS(MADERAS, N, M, L, K) << endl; 
            else

                if(SUMAS(MADERAS, M, N, L, K)==-1)
                    cout << "impossivel" << endl;
                else
                    cout << SUMAS(MADERAS, M, N, L, K) << endl;

CodePudding user response:

The function is called three times (presumably you're asking only about the first if statement). If the definition of the function is available to the C compiler, and the C compiler can prove to itself that eliminating one or more of the function calls has no observable effects, the compiler may choose to optimize away the function calls.

C allows any optimization that has no observable effects. The C compiler is not obligated to effect the optimization, if one were possible.

So, the answer is to how many times the function will actually be called cannot be determined based on the information shown in your question. If the function's definition is not available then the compiler must generate code that calls the function every time, and that settles the issue. If the definition of the function is available, but it is not possible to prove that eliminating one of the function calls has no observable effects: same thing. If it is possible to prove that, then it's entirely up to your compiler.

CodePudding user response:

The function (or macro) is called every time.

  • Related