
Yesterday I had to teach the distinction between pass-by-reference and pass-by-value to my first year non-CS students. There are second and third year CS students that don’t grasp this concept. But there you go.
And so I explained it, to a sea of blank faces. They understood what I was saying (I think) but it made no sense to them. Trying to explain that the same thing can have different names, but sometimes it’s a different thing and sometimes it’s just the same value as the other thing but… yeah. It’s a little mystifying.
So I hit upon a better way to explain this (the slight temperature from the swine flu vaccine I’d had the previous day and the copious amounts of Tylenol probably helped), using Ice-Caps, which one of my students keeps telling me I should be drinking (I know, instead of tea – horrifying!). Lets call her Student A.
So Student A can have an ice-cap, which she calls delicious. Then she can pass it to me as a parameter, and I can call it disgusting-like-poutine. Then I can pass it to Student B, and she can call it n’apporte quoi. If we give it back to Student A, she can continue calling it delicious.
If we’re passing by reference then it’s always the same Ice Cap. Student A might have a full one, drink a bit, give it to me, I might pour some away, then my function exits and Student A has the Ice Cap again (in some sense Student A always has it, but let’s just assume that we as a group are running in a single thread), which has whatever was left when I finished pouring it away.
If we pass by value then each of us has our own Ice Cap. When Student A gives me the Ice Cap, it’s like she gives me a clone of hers. If she passes me a full one, and I pour all mine out, she still has a full one when I exit. My actions to the Ice Cap do not affect her Ice Cap.
Make more sense? The problem with teaching programming is that everything seems very abstract. I find adding real world examples (and humor) really helps.
5 replies on “Explaining Pass by Reference and Pass by Value”
It wasn’t until I went back after graduating and read my basic Java textbook again that I realized there was such a thing as pass-by-value and pass-by-reference.
I think the harder thing to describe is that Java always passes by value, and the variable that we think passes a reference to an object actually just passes a value (a memory pointer) that references an object that exists somewhere in memory.
It wasn’t until I went back after graduating and read my basic Java textbook again that I realized there was such a thing as pass-by-value and pass-by-reference.
I think the harder thing to describe is that Java always passes by value, and the variable that we think passes a reference to an object actually just passes a value (a memory pointer) that references an object that exists somewhere in memory.
[…] Explaining Pass By Reference and Pass By Value […]
I know, it’s a really difficult concept – I was *amazed* they were teaching it to first year management students!
I know, it’s a really difficult concept – I was *amazed* they were teaching it to first year management students!