Re:Ask HN: How does the return statement of a function work?
by cpach on Monday April 10, 2023 @04:04AM
(#35510168)
“(…) this question better fits stackoverflow and likely already answered there, in much more detail than HN could do.”
By now we have an answer that explains CPython’s assembly, so I think we’re on par. Considering the amount of articles on HN related to business, culture, history, etc, IMHO it can’t hurt with some programming questions every now and then.
Re:Ask HN: How does the return statement of a function work?
by wruza on Monday April 10, 2023 @04:23PM
(#35516940)
No, I think it’s perfectly fine to ask. I was talking from a viewpoint of chances to get an answer. Some threads here just sink too quickly, while SO specializes in being Q&A-purposed, at least in my experience there.
The variable no longer exists, but the value does. It depends on a language/runtime, but it’s safe to think about it in terms of “roots” which are non-collectible. A stack (up to the tip) or a special register is one of the roots. Globals table/object is also a root.
When you pin a value (not a variable) into a root, e.g. by putting it on stack (as `return x` does) or by setting it into a global, you prevent it from being destroyed.
Also, this question better fits stackoverflow and likely already answered there, in much more detail than HN could do.