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

  • (11 Pages)
  • +
  • « First
  • 9
  • 10
  • 11

154 Replies - 9437 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

#151 jimblumberg   User is offline

  • member icon

Reputation: 5678
  • View blog
  • Posts: 17,446
  • Joined: 25-December 09

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

Posted 15 November 2017 - 10:31 AM

Right, utter nonsense.

I suggest you study up on pointers, since you seem to have no clue as to what is happening when you use a pointer.

Perhaps this link may be a good place to start.

And from that link:

Quote

Pointers used as Writeable Function Parameters

When a function or subroutine is called in C, a copy of its parameters are passed to the function. For example if

    int Variable1=96;
    Subroutine1(Variable1);
    printf("%d",Variable1);


is called on this function

    void Subroutine1(int Parameter1)
    { printf("%d",Parameter1);
      Parameter1=Parameter1+1;}


then only ‘9696’ will be printed , not ‘9697’, because only the local copy, ‘Parameter1’, of the value of ‘Variable1’ in the subroutine, not the original variable itself, has been altered. In some languages, like FORTRAN-77, it is the variable itself which is as the parameter so the equivalent of this subroutine would work.

Sometimes a copy is what is desired (particularly if one wants no risk of a subroutine from someone else changing the variable or if the routine is to be used recursively) and sometimes the variable itself is desired (particularly if the original variable needs to be altered). C allows the choice. Unfortunately it does not offer the choice in a clear manner like Java where they are ‘in:’, ‘out:’ & ‘inout:’ as per the directions the data goes (the C in-only method being equivalent to ‘in:’, the FORTRAN-77 both-ways one equivalent to ‘inout:’). C instead, as you have now come to expect, bodges it up with pointers.

If one has a pointer to the variable instead of the variable itself as the parameter then, even though that pointer gets copied, one can use its value, the memory address, to directly access the memory where the value of the original variable is stored.

So for example altering the above example to
    int Variable1=96;
    Subroutine1(&Variable1);
    printf("%d",Variable1);


and

    void Subroutine1(int *Parameter1)
    { printf("%d",*Parameter1);
      *Parameter1=*Parameter1+1;}


will produce ‘9697’.

This is a rather messy thing to do in that requires altering not just the function parameter list & the calling line but every reference to the parameter in the function but that is the way C does it.


And from the above quote:

Quote

If one has a pointer to the variable instead of the variable itself as the parameter then, even though that pointer gets copied, one can use its value, the memory address, to directly access the memory where the value of the original variable is stored.



Jim

This post has been edited by jimblumberg: 15 November 2017 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#152 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11430
  • View blog
  • Posts: 19,491
  • Joined: 19-March 11

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

Posted 15 November 2017 - 11:11 AM

View Postccdan, on 15 November 2017 - 09:57 AM, said:

If it's not passed into a function, then it can't be processed by a function.


You've insisted on this, but it's not clear to me why. Can you explain why you think this is so?

Quote

A display function wouldn't be able to display an object if the data that forms that object would find itself, at some point, inside the function body.


I'm not sure what it means for an object to be "inside the function body". For example, let's suppose I'm working on a codebase that makes use of an Image object which knows how to display() itself - it has a function by that name, and when that function is called (perhaps with some parameters), some code is executed and the image is displayed.

Now let's suppose that I have a function do_some_stuff_and_then_display(image) which takes a pointer to an image (that is, to an instance of Image) and does some stuff, and eventually calls the display function of that instance.

Now, clearly the image is displayed - that's what happens when we call display. So is the Image instance "inside the function body" in your view?
Second question: the Image.display function clearly "can display" the object, but it's not clear to me that the object is, at any point "inside the function body" in that case either. Can you try to resolve my confusion on what it would mean for an object to be "inside" an instance method which is, in some sense, "inside" the class?
Was This Post Helpful? 0
  • +
  • -

#153 ccdan   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 117
  • Joined: 09-December 12

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

Posted 16 November 2017 - 03:55 AM

View Postjimblumberg, on 15 November 2017 - 07:31 PM, said:

I suggest you study up on pointers, since you seem to have no clue as to what is happening when you use a pointer.

Yet you fail again to tell me what's wrong with what I said. You keep coming with irrelevant code.

View Postjimblumberg, on 15 November 2017 - 07:31 PM, said:

C allows the choice. Unfortunately it does not offer the choice in a clear manner like Java where they are ‘in:’, ‘out:’ & ‘inout:’ as per the directions the data goes (the C in-only method being equivalent to ‘in:’, the FORTRAN-77 both-ways one equivalent to ‘inout:’). C instead, as you have now come to expect, bodges it up with pointers.

Not sure what the author is trying to say. I'm not familiar with FORTRAN, but C offers pretty much anything Java offers(and more) in terms of passing references/variables, you just have to use the right syntax and data types.

View Postjimblumberg, on 15 November 2017 - 07:31 PM, said:

to directly access the memory where the value of the original variable is stored.

[/quote]
That "access" is what I call pass-by-reference.

By the way, pass-by-reference in C is best done with pointers to the data, not with non-pointer primitives.

When you pass a primitive int to a function that has pointer to an int as a formal parameter, the pointer will take address of the passed variable, not the location where the variable will point to(impossible with primitive). The function will use the external variable to make changes where the variable points to.

When you pass a pointer to an int, to a function that has pointer to an int as a formal parameter(as above), the formal parameter pointer will get the address where the passed pointer points to, not to the address of the pointer(that can be done, too, with pointers to pointers eg. int**) Thus, the function will read and write directly to the memory location where the passed pointer pointed to.
Was This Post Helpful? 0
  • +
  • -

#154 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6774
  • View blog
  • Posts: 23,083
  • Joined: 05-May 12

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

Posted 16 November 2017 - 05:04 AM

View Postccdan, on 16 November 2017 - 05:55 AM, said:

When you pass a pointer to an int, to a function that has pointer to an int as a formal parameter(as above), the formal parameter pointer will get the address where the passed pointer points to, not to the address of the pointer(that can be done, too, with pointers to pointers eg. int**) Thus, the function will read and write directly to the memory location where the passed pointer pointed to.

No it doesn't. The caller has to explicitly pass in a pointer to the integer, and the called function has to know to explicitly dereference the pointer. The pointer is passed by value. The colloquial "pass by reference" here means that the pointer is passed around, and there is an understanding between the caller and callee about how to deal with the pointer that is passed between them. All of this "understanding" is on the part of the programmer. It's not something that the C language performs automatically.
Was This Post Helpful? 0
  • +
  • -

#155 jimblumberg   User is offline

  • member icon

Reputation: 5678
  • View blog
  • Posts: 17,446
  • Joined: 25-December 09

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

Posted 16 November 2017 - 08:22 AM

Quote

Yet you fail again to tell me what's wrong with what I said.

Let's be sure we're talking about the same nonsense.

View Postccdan, on 15 November 2017 - 08:57 AM, said:

View Postjimblumberg, on 14 November 2017 - 06:47 PM, said:

No matter what you do the original object is never passed into a function.

If it's not passed into a function, then it can't be processed by a function. A display function wouldn't be able to display an object if the data that forms that object would find itself, at some point, inside the function body. The same with a function that modifies a value via a reference. That value would have to get inside the function in order to be modified by some mechanism(for example a conditional increment operation)


Quote

Yet you fail again to tell me what's wrong with what I said. You keep coming with irrelevant code.

Okay, I have already told you that the entire statement you made was utter nonsense. But let's break it down a little farther.

Quote

If it's not passed into a function, then it can't be processed by a function.

That "it's" refers to the original data which is never directly passed into any function, no matter if you pass by value or pass by reference. When you pass by value the compiler copies the original data and passes that copy into the function. When you pass by pointer the compiler passes a copy of the pointer into the function, not the original object, which is used to refer to the memory location where the original data is located by dereferencing the pointer.

Quote

A display function wouldn't be able to display an object if the data that forms that object would find itself, at some point, inside the function body.

Again nonsense. The function can derefernce the pointer to access memory where the object resides.

Quote

The same with a function that modifies a value via a reference. That value would have to get inside the function in order to be modified by some mechanism(for example a conditional increment operation)


Again nonsense. The original value never gets passed into the function, in this case the compiler creates an alias to the original data. This alias is basically a wrapper around a pointer to the address of the object. This alias is then copied and passed to the function.

Quote

You keep coming with irrelevant code.

Where did I come up with any irrelevant code? First the code is relevant, but since you don't seem to understand pointers it may not seem relevant to you.

Quote

Not sure what the author is trying to say. I'm not familiar with FORTRAN, but C offers pretty much anything Java offers(and more) in terms of passing references/variables, you just have to use the right syntax and data types.

Perhaps it would be clearer if you just ignored the references to the other languages.

Quote

Sometimes a copy is what is desired (particularly if one wants no risk of a subroutine from someone else changing the variable or if the routine is to be used recursively) and sometimes the variable itself is desired (particularly if the original variable needs to be altered). C allows the choice. Unfortunately it does not offer the choice in a clear manner. like Java where they are ‘in:’, ‘out:’ & ‘inout:’ as per the directions the data goes (the C in-only method being equivalent to ‘in:’, the FORTRAN-77 both-ways one equivalent to ‘inout:’). C instead, as you have now come to expect, bodges it up with pointers.


Quote

Quote

View Postjimblumberg, on 15 November 2017 - 07:31 PM, said:
to directly access the memory where the value of the original variable is stored.
That "access" is what I call pass-by-reference.


Again your selective reading is kicking in. By ignoring the rest of the quote you're missing important information. Again your unfamiliarity with pointers is a big problem here, which is why I suggested you study up on pointers.

Quote

If one has a pointer to the variable instead of the variable itself as the parameter then, even though that pointer gets copied, one can use its value, the memory address, to directly access the memory where the value of the original variable is stored.


You are not directly accessing the memory, you are accessing the memory through the pointer. The pointer is the key to passing by reference. You are not passing the original object into the function, you are passing a copy of a pointer to the original object.

Quote

By the way, pass-by-reference in C is best done with pointers to the data, not with non-pointer primitives.


The only way you can pass by reference in C is by using pointers, that is what pass by reference means in C.

Quote

When you pass a primitive int to a function that has pointer to an int as a formal parameter, the pointer will take address of the passed variable, not the location where the variable will point to(impossible with primitive). The function will use the external variable to make changes where the variable points to.

I really don't understand your point here. In C or C++ "primitives" are objects. These "primitives" are not treated any differently than other objects.

Jim
Was This Post Helpful? 1
  • +
  • -

  • (11 Pages)
  • +
  • « First
  • 9
  • 10
  • 11