Hacker News new | ask | show | jobs
by skrishnamurthi 1158 days ago
You are not at all alone in having this confusion. In fact, you are farther along than many learners in having gotten this far and being able to articulate your confusion so clearly.

The problem is with the statement `return a`. It should be read not as it's written, but rather as `return the value of a`. Once you realize that, much of your confusion will be resolved.

Over the past several years, my research group has studied and documented these kinds of programming misconceptions. We have abstracted them into a set of key issues we find people getting confused about in the basics of programming semantics that cut across languages like Python, JavaScript, OCaml, Racket, etc. — what I call "SMoL", the Standard Model of Languages.

My PhD student Kuang-Chen Lu has created an excellent SMoL Tutor that is a self-guided tool for learning these concepts clearly and addressing misconceptions. Please go to https://www.plai.org/ and pick either of the version 3.2.2 books, and in there go to page number 14/PDF page 15, "Teach Yourself SMoL". It provides links to all the tutors in order. Based on past student experience, you will need about 20 minutes per module. That's why they are broken down into several modules; so each one is relatively quick and you don't have to spend hours at one sitting.

We welcome feedback on the Tutor after people have done it.

1 comments

> 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!

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.