4 ms·
You should read up on "Pass by Value" and "Pass by Reference" and stack vs heap. In Python everything is created on the heap which means it exists until it is g
by blindriver 4y ago
You should read up on "Pass by Value" and "Pass by Reference" and stack vs heap. In Python everything is created on the heap which means it exists until it is garbage collected. When you return the list, you're actually passing back the memory address which is why it is accessible.
In a language like C which is Copy-by-value, if you are returning a struct, it will make a copy of the struct and pass that back to the calling code. It's actually a bit more complicated than that when you deal with pointers or structs that have pointer members, but it's all still essentially copy by value.
> Once the function call has completed execution, it no longer exists in memory.
This is the part that is wrong. If it's created on the heap it still exists. If it's created on the stack like a local variable in C, then it won't be after the function returns. In Go, it will figure out if the variable should be created on the stack or the heap.