3 ms·
>3. A lot of my need for goto statements vanished with nested functions. How does that work? Not immediately obvious to me.
by vram22 9y ago
>3. A lot of my need for goto statements vanished with nested functions.
How does that work? Not immediately obvious to me.
- WalterBright 9y agoA lot of the goto's were used to use a common sequence of code followed by a return. With nested functions, the nested function contains the common sequence of code, and you just: T common() { ...common sequence of code... } ... return common(); instead of: goto Lcommon; ... Lcommon: ... common sequence of code ... return result;
- vram22 9y agoGot it now, thanks. But, this (the "return common();") could be done even if the function "common" was not nested within the current function, but defined outside of it, right? So what is the benefit of defining "common" as a nested function? Is it because it then has access to, and can use, variables defined in the outer function?
- WalterBright 9y agoYes, you can do it that way. But then you'll need a "context pointer" to pass references to the locals it'll need, and the function will need to be located "someplace else", meaning it is not encapsulated. I've done that for years with C and C++. Nested functions are so much nicer and clearer.
- vram22 9y agoGot it now, thanks. I've used nested functions in Python and they have some good uses. Will check them out in D.