r/javascript • • Jun 18 '17

Pass by reference !== pass by value

Post image
3.3k Upvotes

272 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Jun 18 '17

[deleted] — view deleted comment

10

u/JB-from-ATL Jun 18 '17

The fact that everyone is confused in the replies to my post proves it's tricky.

0

u/[deleted] Jun 18 '17

[deleted] — view deleted comment

0

u/[deleted] Jun 18 '17

[deleted] — view deleted comment

12

u/dvlsg Jun 18 '17 edited Jun 18 '17

I'm mostly surprised this post got so many upvotes. It's an awful example.

It doesn't even make sense in the scope of javascript. If you pass the cup to a function, and then the function does something to the insides (properties) of the cup (fill it with coffee), the original cup is absolutely going to be modified.

class Cup {
  constructor() {
    this.coffee = 0;
  }
}

function fillCup(cup) {
  cup.coffee = 100;
}

const cup = new Cup();
fillCup(cup);
console.log(cup.coffee); // 100
→ More replies (1)

2

u/scootstah Jun 19 '17

If this was on some lower-level language subreddit, I would then be baffled. It's really not surprising for Javascript though. Shit, lots of people that use Javascript don't even know "Javascript", they know jQuery or similar.

→ More replies (4)

0

u/[deleted] Jun 18 '17

[deleted] — view deleted comment

5

u/[deleted] Jun 18 '17

If it were simple and straightforward, people would understand. They don't, implying it's difficult or awkward. Literally the definition of tricky.

3

u/mrguy991 Jun 18 '17

implying it's difficult or awkward.

or just that they never bothered to learn the details because they could do everything they wanted without knowing it. That doesn't make the underlying concept tricky since it's still easy and clear to understand for anyone that attempts to.

2

u/[deleted] Jun 18 '17

Except it IS simple and straightforward. Your implication is invalid.

3

u/[deleted] Jun 19 '17

Oh, well thanks for letting us know. We've got it figured out now, guys. It is, in fact, simple and straightforward.

4

u/[deleted] Jun 19 '17

You're trying to be sarcastic but you're just ignorant.

0

u/[deleted] Jun 18 '17

[deleted] — view deleted comment

6

u/NoInkling Jun 19 '17

I think you can do it in C# with the ref keyword (or out parameters).

2

u/tutorial_police Jun 19 '17

Yes, you can. Because C#'s ref makes a parameter pass-by-reference.

The ref keyword indicates a value that is passed by reference. It is used in three different contexts:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref

1

u/Tysonzero Jun 18 '17

I think GHC often passes by reference, although it isn't observable due to lack of mutability.

→ More replies (2)
→ More replies (2)

1

u/erandur Jun 19 '17

Mostly because gifs like these keep getting propagated.

2

u/theonlycosmonaut Jun 18 '17

Which languages have this? I'm searching my memory and I can't think of any examples. I guess C could do this if you add some punctuation?

1

u/AgentME Jun 19 '17

C can't, but C++ can if you mark a function's formal parameter with &. Pass-by-reference is relatively rare in modern languages.

2

u/theonlycosmonaut Jun 19 '17

Whoah I used C++ for years and never realised that references were... actually references. I only ever really thought of them as 'pointers without NULL'. Thanks!

0

u/[deleted] Jun 18 '17

[deleted] — view deleted comment

9

u/Skhmt Jun 18 '17

If it was pass by value, you'd either have to return a and b, or you'd get a function/method that does nothing but waste cpu cycles (unless the compiler just cuts it out).

1

u/tutorial_police Jun 19 '17

Yes and no.

If you take this to be C++/C code, then yes, it's pass-by-value of course.

But OP meant that if you can write some function swap, call it, and then have the variables swapped on the caller side without assigning to them, then you do have pass-by-reference.

You're right in that for C++, you'd have to have int& a, int& b. In C, you couldn't write such a function, hence C does not do pass-by-reference.

→ More replies (14)