Hacker News new | ask | show | jobs
by noob_eng 1158 days ago
> It should be read not as it's written, but rather as `return the value of a`

So values that are returned need not be primitive values like basic ints, floats, strings, etc? They can be complicated structure values also?

Got it, then!

2 comments

This also depends on a language. If we talk about python, every structure (dict, array, object) and value (string, number) is an object, and variables can only reference these objects (internally by hw address into the “heap”, which is not visible in python). So when you return a python array, only its reference is returned. And that reference, among others possibly, prevents an actual object from being destroyed. Similar in js, perl, ruby, vb, other safe languages which never expose references (pointers).

But in e.g. C, structs and arrays can also be allocated right on stack, which may be, and often is, preferable performance-wise. So their variables are their values, without referencing, aka “automatic storage”. Like in, 700 byte-sized struct right in stack memory. You can return such struct in C, it will be copied or “moved” as described elsewhere itt. But you cannot return an automatic array, because it decays to a pointer to stack memory that gets reclaimed right after return, and accessing through such pointer is a programming error. To deal with this limitation you either return a pointer to a heap—allocated array, or accept an array as an argument instead, offloading the storage class responsibility to the caller. Same thing people do with structs really, except for very small ones, to avoid copying and related issues.

> So values that are returned need not be primitive values like basic ints, floats, strings, etc? They can be complicated structure values also?

It depends on the language and the rules about what structures can hold.

For example, say your function doesn't return a value, but returns another function, and that function refers to variables in the first (this is called a "closure" or "lambda", the variable is "captured" or "closed over" by the returned function).

In some languages this is OK and normal to do, in others there's no guarantee that the original variable exists anymore and the behavior is "undefined", and in others it is forbidden.

The way that it is implemented also depends on the language.