Situations like this are exactly where nested functions can be helpful.
I've always thought that it was a shame that C didn't have them.
Sometimes I almost wish that Algol flavored languages like Pascal anf Modula 2 would have won out for systems programming, instead of C and the languages it inspired.
Actually, GNU C supports nested functions, and a new round if standardization is just starting up, so maybe there's a chance?
In languages where braces define a scope even if there's no if, for, etc. keyword around, you can get a lightweight version of that just by sticking braces around your "paragraphs", as needed. They aren't the same as nested functions, in particular because you can't invoke a naked block multiple times, but if you've got a long function that hasn't got any useful break points in it, but you just want to chunk things, curly braces may help you isolate things nicely.
I tend to consider this a "last resort" over using actual functions, but there are certain classes of functions where this is helpful. The two biggest I know of are functions that represent a state machine, and functions that are the big top-level "plumb all the libraries together" where breaking that up into separate functions significantly complicates code due to all the intricate routing of values you have to do.
> They aren't the same as nested functions, in particular because you can't invoke a naked block multiple times,
Sure you can, just use `goto`! It's fantastic for code reuse. ;p
On a more serious note, gcc and clang both support block functions now. Pretty sure they aren't full closures but they can be handy in these situations.
The thing is, naked scope blocks are missing the main reason I'd use a nested function: the ability to be named.
I do occasionally use scope blocks when I want to constrain the scope of one or more local variables, and there isn't an otherwise appropriate scope already created by a flow control construct.
I also use naked scope blocks sometimes. To me, their main purpose is to make temporary variables go out of scope, to clarify which variables are still intentionally live beyond the block.
I've always thought that it was a shame that C didn't have them.
Sometimes I almost wish that Algol flavored languages like Pascal anf Modula 2 would have won out for systems programming, instead of C and the languages it inspired.
Actually, GNU C supports nested functions, and a new round if standardization is just starting up, so maybe there's a chance?