I know that if I have a function call as a condition it will run the function everytime the condition is checked but what if I had a variable with a function assigned to it and I put the variable in the condition, would it execute the function everytime the condition is checked? Or does it just execute once and keep the return value of the function
#conditions with variable or function
12 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
it will run the function everytime the condition is checked
not necessarily. it depends on said condition. short circuit may be kicking in (again, depends on said condition)
had a variable with a function assigned to it and I put the variable in the condition, would it execute the function everytime the condition is checked?
can you provide an example?
as a side note - do note that if said function can be evaluated at compile time, depends on your compiler optimization settings - the functions may not be called at all
he means that for example
int a = foo();
for(size_t i = 0; i < 10; i++){
if(a>10){
printf("foo");
}
}
would call foo() 10 times or not
and it wont
@tulip reef i guess that is what you asked?
idk what else could the question be directed at
you are copying the return value of the function into a variable
not aliasing the function
🤷♂️
It would be beat to let them answer instead of guessing
There are two ways for functions to interact with variables. You can either store the return value
int value = getValue();
or you can store a pointer to the function, in order to run it later.
int(*function)() = &getValue;
int value = function();