Let's try an explanation of print() vs. return using tapes and switches.

At the simplest a computer has:

1. An input tape.
2. An output tape.
3. A code tape.
4. A memory bank.
5. A stack bank.

en.wikipedia.org/wiki/9_track_t…
For all you youngins who never used VHS tapes, Sony Walkmans, a film real, or loaded your video games into your C-64 with a tape, then it's kind of like if someone wrote 1s and 0s on a roll of toilet paper as they rolled it onto another roll, so to read it you have to unroll it.
The tape dominates all concepts of computing. You wrote data and read to tapes. You printed output to rolls of paper on giant typewriters (printers), which is basically a paper tape. There were even paper tapes. Their defining characteristic is they are: LINEAR.
Remember the roll of toilet paper with numbers on it? What would you have to do if I told you, "Hey, go find out if sheet #345 on that roll is a 1 or a 0."

Yes, you would have to unroll and look at sheet 1 to 344 before you could see what's on 345.
All of your early computers got their input from some kind of input like this: It was always a linear input like a tape. It could be punched cards put into a hopper:

en.wikipedia.org/wiki/IBM_1402

You put in card 1-400, it processes then one at a time, and your program loads.
Tapes eventually replaced these card hoppers, but still operated linearly. Output can also be written to a tape, or printed to a printer, and that was actually what passed for a "screen" in the early days.

Yes, people printed the output because they didn't have screens yet.
I'm betting a ton of you *also* don't have a printer, but I'm going to be solid money only 0.0001% of you actually have one of these monstrosities:

columbia.edu/cu/computinghi…

You see this guy loading that long piece of paper? It's actually a "paper tape" called "fan fold paper".
You feed that in, and the edges have holes where teeth can feed it in, then something like a typewriter head smashes it with an ink strip to make a letter. It's basically an automated typewriter, but it comes from the Jacquard machine:

en.wikipedia.org/wiki/Jacquard_…

A *lot* does.
This is why your file APIs have weird things like "seek()", "rewind()", "set()", "read()". Those are all tape operations. If you can find old tape players they didn't call it Fast Forward? They called it "Seek" because a tape is linear so you have to "search/seek" through it.
This thing is why you write "print()" to put text on the screen, and why it's linear one-line-at-a-time. It comes from paper printers (which come from typewriters):

en.wikipedia.org/wiki/IBM_Selec…

Once you smash metal into ink onto paper that's it. Time to move on to the next line.
That's also why you have "\r" and "\n" for "carriage return" and "line feed". Those are typewriter terms. You had the ENTER key (line feed) which moved down one line, then you smacked a big lever to make it go back to columns 0 (carriage return).
So input can come from tapes, but it also comes from the keyboard. Those weren't a thing for a while so even your keyboard is treated a lot like a tape--just a linear stream of characters flowing in.

The problem with tapes is they are SLOWWWWWW because they're linear.
To work with the data on input tapes (or keyboard) you'd need a faster *random* way to store temporary data. If you wanted the character at position 1 billion on a tape you'd have to read all 1 billion characters.

But, what if you have a bunch of light switches?
That's a transistor and the used to be tubes like you find in my guitar amp. They're a switch you can control with electricity kind of like going "Alexa turn on my bedroom lights" if you can get that to work.

So if you put a ton of these in a grid, you can "store" 1s and 0s.
Let's say you have a board with 8 real light switches like in your house, and each one controls a real light. If I tell you "store the number 01 10 00 01" you would flip the switches that match 1 for "on", and switch the others for 0 for off.

Shrink that down and you have RAM.
Random Access Memory (RAM) is like a massive wall of light switches that you can use to turn on 1 or 0 by flipping the switch, except it's all controlled by electricity when the computer wants to store a piece of data. It works kind of like this:
1. It reads a number from the input tape, like 0101.
2. It then finds a set of 4 switches in the "bank" and flips off 0, on 1, off 0, on 1.
3. The CPU can now randomly read and write this bank because it's not a linear tape anymore. It just needs to know the switch address.
So the computer's RAM bank of switches is what's changing when you write this:

x = 1

Now the paper printer output tape is what changes when you write:

print("I kill trees")

It's just your terminal is being treated like this paper printer.
To put it another way:

x = 1

Flips switches on and off in a giant bank of light switches.

print("Howdy y'all")

Tells glorified IBM selectric to smash a strip of ink with a ball of metal letters onto a piece of paper with:

H o w d y y ' a l l
We've got medium to read data from that's a tape (tape, keyboard), and a way to output the results of computation for storage and view (tape, paper, terminal).

We've also got a way to keep fast temporary data while we work on it in the form of electric switches.

One more thing:
You have code and *somehow* you need to get that code into the computer. The "code" is really just a bunch of 1s and 0s just like tape data, but it gets loaded into the computer and run as instructions. The computer kind of says:

"IF I see #434 that's PUSH."
Rather than invent some new "code data" or "code input" we got smart and just started using tapes and RAM to store and process code. Code really is data. You read it in off a "tape" (aka a file, keyboard, or tape), and you store it in RAM for processing. You can even change it.
Now, your code is processed as kind of a separate stream from the data, and the reason the code needs to be in random access is because the computer has to "jump" to different spots in RAM to do things like loops and if-statements. It's like:

1: print x
x = x-1
if x > 0
jump 1
Now this was how everyone wrote code for a long time but it's too error prone so we came up with a "function jump", or "call return". In this special jump you say:

save the code location
jump to 1
at the end of 1, jump back to the code location

How does it save it though?
It uses the STACK. The computer has a special spot in the RAM where it can "push" things on, like where was before a function jump, and the "pop" things off to return back.

If you have 1000 function calls, you'll get 1000 pushes of each location, and 1000 pops to go back.
You can also push the variables for the function onto this stack. If you have function call like this:

dostuff(a, b, c)

Then the computer might do:

push a
push b
push c
push this_location
jump dostuff

At the end of do stuff it'll do:

pop a
pop b
pop c
pop return
jump return
In fact, you can see something like this with Python's dis library. LOAD is kind of like pop, CALL_FUNCTION is the function jump, LOAD_CONST is a push, and RETURN_VALUE is the "jump return".
In our little simple computer this stack could just be a reserved part of RAM to keep things simple, but the point is this:

return is all about the stack. push the return value on, pop the code location to go back.

return *DOES NOT* make this typewriter head smash ink on paper.
Conversely:

print is all about making a typewriter head smash ink on a linear tape of paper (now your terminal screen).

print has nothing to do with flipping these little light switches on/off or pushing/popping/jumping with the stack.
A final way to put it is this:

If you think that:

print("hello world")

and

return "hello world"

are the same thing then you're confusing a stack of paper with a fancy light switch.

Hopefully that helps you remember the difference, or is at least entertaining.
If you'd like to study a tiny little machine like this that's written in <200 lines of JavaScript you can look at my little Buttons project:

git.learnjsthehardway.com/zedshaw/button…

That's the code, and you can play with it at:

buttons.computer

Manual at:

buttons.computer/manual

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Zed A. Shaw, Writer

Zed A. Shaw, Writer Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @lzsthw

2 Jun
I have a couple of new gotchas for people doing Svelte:

1. fade:out, or almost any "out" transition effect is death. Avoid at all costs.

2. {#await} on a promise is great, but if you update the promise it reloads the *entire* DOM at that point. Put it into a component.
For #1, the demo involves this fade:out on the Spinner I use to show the video is loading. Seems harmless *until* you want to transition to an entire new page/component, say with svelte-spa-router.
You can see it happen here. I click the link for a related video, and you can see the current video has to "fade out", preventing that page from leaving the DOM, so it looks like both pages load at the same time. I'm at a point now where I don't use *any* out transitions.
Read 10 tweets
1 Jun
I've been playing different Ark islands to see what I like, but I still can't figure out why people like this game. I get it, you have dinos and it's got a lot to it, but it's incredibly unbalanced almost to the point of being unplayable without excessive cheats. For example:
In playthrough on Aberration I had a Basilisk (ark.fandom.com/wiki/Basilisk) find my base in the *beginner easiest zone* and camp out there. To kill one you need to be about level 100 and have another creature called a Rock Drake (ark.fandom.com/wiki/Rock_Drake) plus Hazmat gear.
I was a little level 20 newbie who had only built a base and tamed one dino, so what the hell kind of game has the most powerful creature in the game killing starting players? I ended up having to use god mode to kill the thing and it still took about 20 minutes at my level.
Read 15 tweets
31 May
Spent a whole day figuring out a weird bug with svelte-spa-router only to find out, it's "on purpose" (which is programmer for "ooops uhhhh yeah I totally meant the"):

github.com/ItalyPaleAle/s…

One reason people hate SPAs is things like this. Clicking a link loads a new page.
The end. Full stop. Don't be fancy unless I tell you. You load the page again when I click the link. What's svelte-spa-router do?

Change two variables and then OOPS it's up to you dude to figure out how to make that work like an actual link does everywhere else.
So, with svelte-spa-router you CANNOT treat your svelte routes like pages because of this bug. And it is a bug. You have to treat each route pattern like it's a singleton instance that run onMount once and once only, then you have to sort out changes within that same route.
Read 5 tweets
31 May
Alright, some folks have real answers AND these real answers point out the next stupidity of JavaScript NaN. First up "it's an IEEE standard" is not a defense, but here's the one reply for the reasoning:

stackoverflow.com/questions/1565…

Now...if we're following IEEE then, nope:
1. The IEEE standard is *entirely* about Math equality of floating point numbers. There is *NOT* a specification of === since that's not a math thing, that's a JS thing, so they could have fixed it there.

== is IEEE, fine.

=== is what everyone actually needs.

Then:
2. The IEEE standard is about MATH with FLOATING POINT. So then why is a NaN used when you do parseInt() on a string?

Strings are not floating point. Int is not floating point. If the JS people defend with "IEEE standard" this returning NaN in this situation is a vioaltion.
Read 6 tweets
30 May
So much of CSS is totally half-assed. I add blur on an image when you hover over it. My box-sizing is border box, yet, when the image is blurred I get this garbage blur overflow on the edge. The solution (2nd image) is to..say "overflow: hidden" (line 66)? What? ImageImageImage
How this *should* be implemented is that the blur is contained inside the parent box. The end. If I want the blur to go outside of the parent then I'll put it on *that* component. Nobody in the right mind wants *anything* to bleed outside of boxes accidentally.
But that's CSS all over. Just a half-assed box model that isn't a box model at all, more of a wet-paper-towel model where it seems like the browser devs make a feature and then forget to make it stay inside a box, or forget to make it work inside some random situation.
Read 6 tweets
30 May
Remember how I talked about Ark's Scorched Earth being nothing but a death festival? I've been watching various pro-level players play Ark and there's a certain strategy they use:

1. Die often and forget your stuff.
2. Level up movement speed to out run dinos.

It does work.
Scorched Earth for those who don't know is basically a desert world with unrealistic heat properties. You die in about 4 minutes when the temp is 38c (LOL) but live if you get in a tent and make it 37c. Given that 1/2 of the team is from Egypt I find this doubly hilarious.
Unlike other maps you spawn in places and die almost instantly because of the heat, or cold at night, or millions of deadly dinos. You also can't really escape the heat inside a basic thatch structure--the first kind you can build. You just die over and over with no progression.
Read 11 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(