|
|
|
|
|
by jerf
3647 days ago
|
|
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. |
|
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.