What is the main characteristic of pass-by-reference?

  • (11 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

154 Replies - 7111 Views - Last Post: 16 November 2017 - 08:22 AM

Poll: What is the main characteristic of pass-by-reference? (1 member(s) have cast votes)

What is the main characteristic of pass-by-reference?

  1. The ability of a method to use a reference(like a pointer or something similar to it) and operate on the original data/object without duplicating it (1 votes [100.00%])

    Percentage of vote: 100.00%

  2. The ability of a method to make the passed variable(argument) point to something else (including the ability to swap object or data) (0 votes [0.00%])

    Percentage of vote: 0.00%

Vote Guests cannot vote

#1 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

What is the main characteristic of pass-by-reference?

Posted 23 October 2017 - 05:33 AM

My question comes as a result of the fact that the subject causes a lot of confusion in the Java world, which confusion was caused by James Gosling due to his claim that java "is always pass by value" (which is totally wrong in my opinion, I have a topic on that subject: http://www.dreaminco...alse-nonsense/)
While we don't know exactly what he was thinking, many people believe he said that in order to accommodate c++ developers' view of the subject, very often presented in c++ beginner books as the ability to swap what two variables point to. On the other hand, I'm almost sure that even c++ developers use pass-by-ref mainly in order to avoid duplicating data/objects(that can be pretty large) and not for swapping things.

What's your view on the subject?

This post has been edited by ccdan: 23 October 2017 - 08:58 AM


Is This A Good Question/Topic? 0
  • +

Replies To: What is the main characteristic of pass-by-reference?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,983
  • Joined: 21-June 11

Re: What is the main characteristic of pass-by-reference?

Posted 23 October 2017 - 07:15 AM

Both are obviously important, but a vast amount of C++ code would break if the second weren't possible. On the other hand if the first weren't possible, things would mostly just become slower. So in that sense, the second is more important.

As an obvious example cin >> x; would be completely broken if the value of x did not change. That would break any program that reads input from the user, a file or a network socket.

The same is true in C. If we couldn't take a pointer to a variable (or if pointers to variables wouldn't be able to actually change the value of that variable), scanf("%d", &x); would not work and again many programs that read input would not work.

So the ability to change variables through references is integral to how C++'s APIs are designed and the same is true for C and pointers.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,609
  • Joined: 25-December 09

Re: What is the main characteristic of pass-by-reference?

Posted 23 October 2017 - 07:39 AM

Do you realize that in C or C++ everything is passed by value? Even when passing a pointer you're passing the address of the pointer by value, which means that you can change the pointed to value but not the value (address) of the pointer itself. Even a C++ reference the reference itself is passed by value, again meaning you can change the value of the variable being referenced but you can't change what the reference is referencing.

I'll not comment on the Java aspects too much but I imagine the idea is much the same as in C or C++.

Jim

This post has been edited by jimblumberg: 23 October 2017 - 07:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 23 October 2017 - 08:42 AM

View Postsepp2k, on 23 October 2017 - 05:15 PM, said:

As an obvious example cin >> x; would be completely broken if the value of x did not change. That would break any program that reads input from the user, a file or a network socket.

The same is true in C. If we couldn't take a pointer to a variable (or if pointers to variables wouldn't be able to actually change the value of that variable), scanf("%d", &x); would not work and again many programs that read input would not work.

So the ability to change variables through references is integral to how C++'s APIs are designed and the same is true for C and pointers.

Not sure what you're trying to say, but the things you're mentioning don't change where the original variables(arguments) point to(which is the main point of contention) They only change the content those variables point to(which also happens in java, with non-primitive, mutable, variables)


View Postjimblumberg, on 23 October 2017 - 05:39 PM, said:

Do you realize that in C or C++ everything is passed by value?

No, I don't realize that.

Whenever I hear "pass-by-value" I consider that the value is the content a variable/constant points to. Nothing else, ever.

When you have, say

int i = 5;


the value is the thing(whatever it is, 5 in this case) to the right of the equal sign - that is, the piece of memory the variable points to. Absolutely never anything else.

When you have "pass-by-something" that "something"(which could be a number, an image, etc.) will somehow be transferred to a method so that the method can operate on that data(display a number, an image, or whatever).

When you have pass-by-value, that data will be DUPLICATED and the method will operate on that DUPLICATE, without touching the ORIGINAL.

When you have pass-by-reference, the memory address the passed variable points to will be used by a method, so the method will operate on the ORIGINAL data(that is, there won't be any copy of that data) using that memory address. It's completely irrelevant how that address got there(sure it is copied, there's no other way to do it)

Is my view wrong?

View Postjimblumberg, on 23 October 2017 - 05:39 PM, said:

Even when passing a pointer you're passing the address of the pointer by value

Which is completely beside the point. As long as a method's main purpose is not to operate on the address(read it, display it, make calculations on it, modify it and so on), there's no point in mentioning how that address is passed - since it's only used for operating on the actual data.

This post has been edited by ccdan: 23 October 2017 - 08:43 AM

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,609
  • Joined: 25-December 09

Re: What is the main characteristic of pass-by-reference?

Posted 23 October 2017 - 09:30 AM

Quote

When you have pass-by-value, that data will be DUPLICATED and the method will operate on that DUPLICATE, without touching the ORIGINAL.

Yes this is normal pass by value semantics that is always used.

Quote

When you have pass-by-reference, the memory address the passed variable points to will be used by a method, so the method will operate on the ORIGINAL data(that is, there won't be any copy of that data) using that memory address. It's completely irrelevant how that address got there(sure it is copied, there's no other way to do it)

No its very relevant. Everything is passed by value, period. In this case the pointer/reference is what is copied.

Edit:

Quote

Whenever I hear "pass-by-value" I consider that the value is the content a variable/constant points to. Nothing else, ever.

What happens if the value you want to change is actually the address of the variable?

Jim

This post has been edited by jimblumberg: 23 October 2017 - 09:31 AM

Was This Post Helpful? 1
  • +
  • -

#6 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 23 October 2017 - 10:38 AM

View Postjimblumberg, on 23 October 2017 - 07:30 PM, said:

No its very relevant. Everything is passed by value, period. In this case the pointer/reference is what is copied.

Nonsense. If it's passed by value, then the content of the memory region the passed variable points to, is copied(duplicated), otherwise it's something other than pass by value.

View Postjimblumberg, on 23 October 2017 - 07:30 PM, said:

What happens if the value you want to change is actually the address of the variable?

You are in quite a bit of trouble if you use proper c++ references.

If you want to understand how pass by reference is MOST OFTEN used, maybe you should examine some random source files from various open source projects. You'll have a pretty hard time finding methods that try to manipulate(eg. modify) the actual addresses passed to them via the pass-by-reference mechanism. Addresses are just things that are used to get to the actual content(VALUE) that's targeted by the pass-by-reference mechanism.

Your take is kinda funny, I think it's for the first time I hear someone claiming that everything is passed by value even in C++.

This post has been edited by ccdan: 23 October 2017 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,609
  • Joined: 25-December 09

Re: What is the main characteristic of pass-by-reference?

Posted 23 October 2017 - 11:12 AM

Quote

You are in quite a bit of trouble if you use proper c++ references.

Who said I was talking about C++, much less C++ references? But it is possible even in C++ to change the address of a pointer.


Quote

If you want to understand how pass by reference is MOST OFTEN used, maybe you should examine some random source files from various open source projects.

I already understand how pass by reference is used. Perhaps it is you that should be doing more research. Even in your topic here you're being told basically the same things:

Quote

No if you pass something as * parameter (a pointer thereof) it is still passed by value. A copy of the pointer being passed is made.


Jim
Was This Post Helpful? 0
  • +
  • -

#8 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 23 October 2017 - 11:26 AM

View Postjimblumberg, on 23 October 2017 - 09:12 PM, said:

Who said I was talking about C++, much less C++ references? But it is possible even in C++ to change the address of a pointer.

I was talking about C++ right from the start of this topic.

View Postjimblumberg, on 23 October 2017 - 09:12 PM, said:

I already understand how pass by reference is used. Perhaps it is you that should be doing more research. Even in your topic here you're being told basically the same things:

Quote

No if you pass something as * parameter (a pointer thereof) it is still passed by value. A copy of the pointer being passed is made.


Jim

Well, according to your statements, pass by reference doesn't really exist, because addresses are passed by value, thus "everything is passed by value" (your exact words)

That's not my topic.

What kind of "research" do you think I should be doing? What exactly do you think I don't understand?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 23 October 2017 - 11:32 AM

Why are you being confrontational from the outset?

If this thread just veers to a repetition of the discussion already held in the Java forum (which it is already) then it will be closed.
Was This Post Helpful? 0
  • +
  • -

#10 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 23 October 2017 - 11:43 PM

Let's see if we can clear up some sources of confusion...

Can we agree that the thing being passed is distinct from the thing we are using to accomplish that?

In other words, we have:

pass[some data]
by [using a] reference

Don't you think that the stuff in blue is the actual thing being passed and completely distinct from whatever is being used to accomplish that?

After all, the passing mechanism is called pass-by-reference, and NOT pass-the-reference. Some people seem to conflate the two for some reason.

This post has been edited by ccdan: 24 October 2017 - 12:04 AM

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 01:26 AM

I suppose if you want to pursue the term pass-the-reference then you would also have to consider copy-the-value. However, the existing terms are nicely balanced: pass-by-value, pass-by-reference.

These terms were chosen by someone at some point and have stuck. (I don't know the history, maybe someone just mentioned them in passing but others decided to run with them. This is not uncommon.)

There are plenty of other terms in common usage (both in the computer world and outside of it) that do not stand up to any scrutiny.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 01:34 AM

You could always switch to a functional language where the terms are meaningless ;) because their data structures are immutable:

Quote

In purely functional languages there is typically no semantic difference between the two strategies (since their data structures are immutable, so there is no possibility for a function to modify any of its arguments), so they are typically described as call by value even though implementations frequently use call by reference internally for the efficiency benefits.

Evaluation strategy
Was This Post Helpful? 0
  • +
  • -

#13 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 06:32 AM

View Postandrewsw, on 24 October 2017 - 11:26 AM, said:

These terms were chosen by someone at some point and have stuck. (I don't know the history, maybe someone just mentioned them in passing but others decided to run with them. This is not uncommon.)

The terms may have stuck, but their meanings vary.

Some of the sources that lead to so much confusion:

The C Programming Language - K&R

Quote

In C, all function arguments are passed ``by value.'' This means that the called function is given the values of its arguments in temporary variables rather than the originals. This leads to some different properties than are seen with ``call by reference'' languages like Fortran or with var parameters in Pascal, in which the called routine has access to the original argument, not a local copy.


Pointers on C - Kenneth Reek

Quote

Array names are also passed using call by value, but all that is copied is a pointer to the caller's array)'. When a subscript is used on the array parameter in the function, indirection causes an element in the caller's array to be accessed. Thus, modifying an array element in the function actually modifies the caller's array. This behavior is named call by reference. You can get call by reference semantics with scalar arguments by passing pointers to the arguments and using indirection in the function to access or modify the values.

(the above passage alone might be understood as a contradiction)

C++ Primer - Stanley Lippman & Co.

Quote

When a parameter is a reference, we say that its corresponding argument is passed by reference” or that the function is “called by reference.” As with any other reference, a reference parameter is an alias for the object to which it is bound; that is, the parameter is an alias for its corresponding argument.
When the argument value is copied, the parameter and argument are independent objects. We say such arguments are “passed by value” or alternatively that the function is “called by value.”
[..]
Arrays have two special properties that affect how we define and use functions that operate on arrays: We cannot copy an array, and when we use an array it is (usually) converted to a pointer. Because we cannot copy an array, we cannot pass an array by value.

(this is getting funny already... so "references" work differently than pointers, as if the addresses they get are not copied)

Starting Out With C++ - Tony Gaddis

Quote

When a variable is passed into a reference parameter, the argument is said to be passed by reference.
Another way to pass an argument by reference is to use a pointer variable as the parameter.

[..]
It is also important to learn this technique in case you ever need to write a C program. In C, the
only way to pass a variable by reference is to use a pointer.

(agree!)

How to Program - Deitel

Quote

There are three ways in C++ to pass arguments to a function—pass-by-value, pass-by-reference with reference arguments and pass-by-reference with pointer arguments.
[..]
Reference arguments also enable programs to pass large data objects to a function and avoid the overhead of passing the objects by value (which, of course, requires making a copy of the object). Pointers, like references, also can be used to modify one or more variables in the caller or to pass pointers to large data objects to avoid the overhead of passing the objects by value.
You can use pointers and the indirection operator (*) to accomplish pass-by-reference (exactly as pass-by-reference is done in C programs—C does not have references).


My point of view is very similar to that of the last two authors. On the other hand I find quite confusing the language/terminology used by the other authors.

This post has been edited by ccdan: 24 October 2017 - 06:50 AM

Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13488
  • View blog
  • Posts: 53,879
  • Joined: 12-June 08

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 06:49 AM

Ok.. so what's the end game of the discussion? Authors have varying wording on the interpretation of what is going on?
Was This Post Helpful? 0
  • +
  • -

#15 ccdan  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 09-December 12

Re: What is the main characteristic of pass-by-reference?

Posted 24 October 2017 - 07:05 AM

View Postmodi123_1, on 24 October 2017 - 04:49 PM, said:

Ok.. so what's the end game of the discussion? Authors have varying wording on the interpretation of what is going on?

I don't know. From my point of view, "the end game" regarding this subject in general, would be the end of confusion. That probably won't happen any time soon. Even a reduction in the number of "pundits" who claim to know "the ultimate truth" (usually quoting their favorite author) would be a good start, though I can't see anything in that direction either... there are way too many who try to convince others that "pass by reference" or "pass by value" is exactly this or exactly that ...

This post has been edited by ccdan: 24 October 2017 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

  • (11 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »