Re:Why I Don't Like Golang (2016)
by jolmg on Wednesday June 12, 2019 @08:28PM
(#20170774)
> You can pass functions but not return them.
[ Parent
| on HN ]
Technically passing and returning function pointers is very different from passing and returning functions. This has implications for things like closures.
By the fact that lmm said "You can pass functions", they probably meant "function pointers", since that's the only way you can pass in something that could be referred to as a function in C.
EDIT: At least last I heard. The example I gave is C89, and I know C99 and some GNU extensions. I don't know if a new standard introduced something else that could be referred to as a function that can be passed in but not returned.
A quick google for "c lambda" and "c closure" as well as looking for those keywords in the Wikipedia articles of C11 and C18 turned up nothing, so I guess not.
GNU c can have lambda expressions[1]. And clang does even have closures[2].
1: http://walfield.org/blog/2010/08/25/lambdas-in-c.html
2: https://en.wikipedia.org/wiki/Blocks_(C_language_extension)
Note that the first example relies on multiple GCC-only features, one of which is the atrocity that is nested functions, and even then technically invokes undefined behavior.
GNU C has "downward funarg" closures, like Pascal. They have the same representation as function pointers, which requires the compiled code to generate trampolines (little pieces of machine code that find a PC-relative environment pointer) on the stack. Invoking them requires executable stacks (a linker-level executable option often now turned off by default in major GNU/Linux distros).