How does scope affect the use of nested functions?

I think I have it all, but I would like to confirm.

  • With a dynamic scope, it does not matter if a nested function or a separate function is used, since the variables simply depend on the call stack.

  • When using only pure functions, it also does not matter if a nested function or a separate function is used. This is true regardless of the type of area.

  • With a lexical scope, nested functions roughly mimic a function call with a dynamic scope.

  • With the lexical domain, a program written entirely from pure functions (possibly for one dirty print for standard output) does not require garbage collection. If that matters, I specifically think of GNU C with a nested function extension for this question.

NOTE. By pure function, I mean absolutely pure function: the only thing that is “read” is the parameters, the only thing that is “written” is the return of functions.

thank

+5
source share
2 answers

Are you alright. I would not put it into my brain as a random mnemonic, though - try to understand whys and you will come across lesser surprises.

The documentation on this topic ( http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html ) is pretty good. If you know how the stack works in C, you should understand right away.

Also, the gift I found on sites that might be useful, given your question:

#define lambda(type, body) ({ \
    type __anon_func__ body \
    __anon_func__; \
})

int (*foo) (double) = lambda(int, (double x) { return (int) x; });
+2
source

, , C Lisp, , . , C , Lisps, .

  • , , , , , (Clojure Common Lisp, ).

  • . .

  • , . , , , , , , , . , , , - , .

  • , " "? , , , GNU C , , .

+1

All Articles