News for hackers. Stuff that matters.
Re:Ask HN: How does the return statement of a function work? by wruza on Monday April 10, 2023 @02:48AM (#35509781)
We know that by lexical scoping rules the variable 'a' (which here is a list) is available only to the function body. Once the function call has completed execution, it no longer exists in memory

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.

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.
Re:Ask HN: How does the return statement of a function work? by cpach on Monday April 10, 2023 @04:47PM (#35517196)
I think 'dang made some changes a while ago that makes Ask HN posts stay longer on the Ask page.