It has already been pointed out twice: you want coroutines.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
void thing( bool init ){ static int state; if (init) state = -1; state += 1; switch (state) { case 0: dosomething(); return; case 1: dosomethingelse(); return; default: return; }}