Hey guys,
I've got a state machine in my code (a big switch statement), and in that state machine in several states I need to check to see if a switch has been flipped, and perform an action.. namely light an LED, play sound, and change a mod variable. This check is always the same. So I end up with duplicate code.
In other programming languages I've used, I'd just declare a function within a function, and that function would have access to all the parent function's variables, but I can't do that in C.
I've been told using #defines is bad if you can avoid it, and honestly after having to spend hours debugging one multi-line assembly function because whoever wrote this stupid language decided it was okay for invisible spaces at the end of a line to cause obscure compiler errors, I would tend to agree with that assessment, so I can't use those to fake my function.
I also can't use an inline function, because although the code in those is supposed to just replace the function call, the compiler can decide not to do that which means trying to call variables local to the function holding my switch statement is a no-no.
The only solution that I've come up with so far then is to make my mode variable and my mode constants global, so I can just make a second function and be done with it. And I should probably just do that instead of wasting time trying to get rid of globals since I already have so many, but using globals everywhere just isn't good coding practice. So I'm looking for a solution which avoids that.
So any suggestions?
TLDR; I've got a function with a switch statement inside of it, and inside of that I have duplicated code that I want to reduce down to a single function call without resorting to global variables for the variables said code needs to use. And I don't want to use #define. And inline won't work.