Anyone know how to write a function that controls the execution of code or block of code after the function, like if() and other functions do.
myfunction() {
}
I know I can use...
if(myfunction()) {
}
but I was curious how to control the code execution after myfunction() without having to use it in an if() function.
RogerN
Didn't find your answer? Ask the community — no account required.
J
jeroen Belleman
It'll have to be either an 'if' or a '?:' conditional expression. Basically syntactic variants of the same construct. You could probably abuse a 'for' or 'while', but that would count as obfuscation.
Jeroen Belleman
T
Tauno Voipio
'if (expression)' is not a function. It is the C langauge primitive for two-way branching.
Would you please explain why you're not happy with it?
Tauno Voipio
R
RosemontCrest
First, "if()" is not a function; it is a conditional expression, or a "Control Structure" as Arduino C (Wiring) calls it.
I wonder why you would want or need to avoid using an if() expression, but...
boolean ret_val; boolean myfunction(void);
ret_val = myfunction();
switch (ret_val) { case true: //do some stuff break;
case false: //do some different stuff break;
case default: //initiate self-destruct sequence break; }
;-)
J
John S
Roger -
if(myfunction()) { Some stuff }
Next Statement
If the "if" statement is true, Some Stuff will be executed and then the program will resume with the Next Statement.
If the "if" statement is false, nothing in the curly braces will be executed and your program will fall through to the Next Statement.
Have I understood your question?
M
Maynard A. Philbrook Jr.
I think what you're looking for is to set up a function pointer, one that can be changed at some point.
So what you're looking for is at some point you do your myfunction code and that code could change the actions of the code after that function.
The idea is to later directly execute the below code which has been directed to a specific task at hand.
Look at function pointers in the language, it's a simple matter of either executing a task via an index set from your code at some point or common function pointer with no parameters needed gets updated. I guessing you're trying to set up a constant condition a head of time.
Jamie
S
Spehro Pefhany
while(myfunction()) { }
Will loop as long as the evaluation of myfunction is true
int i; for(i=0; myfunction(); i++) { }
does something similar, but i starts at 0 and increases by one for each loop
do { } while (myfunction());
loops as long as myfunction is true, but because it is tested at the end always executes at least once
of course you can write
myfunction();
which discards the return value, but there may be a useful effect from executing the function.
Best regards, Spehro Pefhany
"it's the network..." "The Journey is the reward"
speff@interlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
R
RogerN
I'm fine with if(expression), just thought there may be an opportunity to learn how, if it were possible, to control the code execution after a function. I see it's done with if(), while(), for(), etc. and if there was a simple way to do this, I could do that instead of if(myfunction()).
I just thought this could be something I missed and I'm interested in learning.
RogerN
M
Maynard A. Philbrook Jr.
I get the impression he wants a function pointer that he can set so that when ever the next block of code, which has a label on it, most likely may contain an address index to a function elsewhere. This is done so that repetitive If, then, else, While etc. do not have to be constantly checking for something that is going to be a constant for some time..
int returnsometingMaybe = (*SomeFunctionPtrThatHasBeenSet) (SomeParameters, Maybe);
Basically, the above code in his example may only need to be executed to change this pointer once in a whie or due to some set user configurations etc..
Jamie
T
Tim Wescott
Except for the conditional expression, you've pretty much listed the possibilities.
I'm not sure what you consider to be simpler than an if statement.
Tim Wescott
Wescott Design Services
http://www.wescottdesign.com
R
RogerN
I can do...
if(myfunction()) { execute some code }
But was wondering if instead I could use...
myfunction() { execute some code }
I'm just trying to see if there was a way (maybe common, I don't have a lot of experience in 'C') to control the execution after a function call to a block of code or resume after the block of code.
I'm not trying to make it complicated, just thought perhaps it was something common that I didn't know about, I'm fine with accomplishing my goal using if().
RogerN
S
Spehro Pefhany
FWIW, the OP is coming from a background of PLCs where the overall program structure is essentially a big for(;;) loop.
Best regards, Spehro Pefhany
"it's the network..." "The Journey is the reward"
speff@interlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
T
Tim Williams
Try...
#define rogerIf {if (myfunction())}
rogerIf { ... }
Or I might have that funny since I rarely use macros.
But no one else will know what the hell you're doing. At best, you might be trying to create a syntax similar to an inner function or something like that, but not working the same way.
Even assembler has the obvious JMP conditionals; everything has flow control statements as such. The real question: why does it make sense *to you* to want to write it that way?
In the main body of your code you can have: . . executing code myfunction() execute more code . .
If myfunction needs to return a value or have a value passed to it, the function definition needs to change to, perhaps:
int myfunction(long x) { execute some code using x return (int answer) }
Then the main body can be: . . executing code z = myfunction() execute more code using z (or not) . .
Does this help?
M
Martin Riddle
You can call the function via an ISR too. If it is a non periodic event then that is the way to do it. eg; and error or fault routine. This looks tranparent too, your main code runs as it normally would.
Cheers
M
Martin Riddle
He could also use a ...
while(1){
if(myfunction()){ break; } // do some other stuff }
applies to the do loop too.
Cheers
T
Tim Wescott
Can't be done.
Your first suggestion is the way it's done:
if (myFunction()) { some code }
Remember that while this is more wordy in the code than what you want to do, those words don't burden the final product -- they just make the code more readable for the poor fools that have to follow in your footsteps, wondering what you did (and believe me, after nine months of working on other things, YOU will be that poor fool).
Tim Wescott
Wescott Design Services
http://www.wescottdesign.com
D
dakupoto
It is rather confusing as to what the final goal is. The code snippet: if(myfunction()) { execute some code } defines conditional execution of 'some code' depending on the results of the execution of 'myfunction'. However, the code snippet myfunction(){ execute some code } declares/defines that 'some code' is executed EACH TIME myfunction() is invoked. Obviously, these two code blocks would not achieve the same goals. Please do not get bogged down with function pointers and function objects. Just have myfunction() return a Boolean value (note that in C, a Boolean value is indicated by a unsigned int 1 or 0, unlike C++, that allows true/false) Then a simple switch-case block or a ternary comparison operator will do the job. Hope that helps.
Join the Discussion
Have something to add? Share your thoughts — no account required.
Didn't find your answer?
Ask the community — no account required
Report Content
You are reporting this content to the moderators. They will look at it
ASAP.