Hacker News new | ask | show | jobs
by oaw-bct-ar-bamf 1775 days ago
I believe that this confusion comes from the avoidance of low level details. A function is just a location in memory where instructions are stored. Returning a function is nothing else as returning a pointer to a memory location.

In languages like Java that want to hide everything memory related I also was struggling with grasping what a function really is. Is it even allowed to return a function?

Languages that hide memory locations and direct memory access are one of the reasons that some programmers are struggling with functions returning functions.

2 comments

Absolutely this. You need that machine language model of how a computer works to really understand programming, otherwise you are just guessing why stuff works.

I'm not capable at all with assembler, can't write my own compiler.

But my model of how a computer works, in my head, is "functions are just sequences of bits arranged as CPU instructions." I know how it works, even though I can't write a compiler or parser.

And it's absolutely essential to how I program, even in the highest level languages (SQL explain plans).

> Returning a function is nothing else as returning a pointer to a memory location.

That depends. If the language supports proper first-class functions, it's basically an instruction pointer plus a record of the variable bindings the function code is "closed over" (i.e. parameterized on). Many languages do support this nowadays.