-1

I know, if we define a function in C++, we can pass, as parameters, pointers. For instance, int function (int* a){ // }. When doing this, we can use that pointer (a) with & and * operators and, this way, be able to modify the original variable passed as parameter.

The latter is also the purpose of passing parameters by reference. However, it's a little bit different. When we call a function declared like this: int function (int &a), a int variable must be passed as parameter and not a pointer as it had been in the previous case. In consequence, inside the second function, we use the parameter a normal way (as a int), but the original value (in the scope where the function is called from) can change.

Many people told me that it's because, when using passing parameters by reference, we are passing, actually, a pointer instead of the value. Is it really true? is there any other explanation? What's the difference between the first and the second function? I would really appreciate your help.

loguepw
  • 35
  • 1
  • 6
  • 1
    It's not really clear what you're asking. You ask "is there any other explanation", but it's not clear what you want an explanation for. – David Schwartz Aug 30 '14 at 05:23
  • 1
    from a C perspective, references are constant pointers with automatic indirection - see http://stackoverflow.com/a/596750/48015 – Christoph Aug 30 '14 at 05:25
  • 1
    If you talking about in `C` then there is not pass by reference concept in `C`. pass by value is there.You can pass pointer to achieve this. – Jayesh Bhoi Aug 30 '14 at 05:25

1 Answers1

3

There are two basic ways you can pass something to a function. You can pass the value of something to a function and you can pass a reference to something to a function. The key difference is that when you pass the value of something, you are essentially creating a new copy with the same value.

In C and C++, pointers are data types that contains a value that is the address of something else. Pointers, like non-pointers, can also be passed by value or by reference in C++.

It's not clear from your question what it is you're trying to compare, but if you're comparing pointers to passing by reference, that's not really a sensible comparison except as two different solutions to a problem. When you pass a pointer in C++, you must also choose whether to pass that pointer by value or by reference.

To some extent, passing a pointer to something by value is roughly comparable to passing that thing itself by reference. You can have a language that only supports passing by value and can "fake" passing by reference by passing a pointer by value. Some languages support only passing by reference, and can "fake" passing by value by passing a reference to a copy.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • To add to the confusion, some languages have "references" that are values like pointers. – Ben Voigt Aug 30 '14 at 05:44
  • @David-Schwartz: I confused pointers and references. I had in mind something like references used pointers. They're different things. Now, I understand how pointers work and You said passing parameters by reference and by using pointers could be compared as two different solutions to a problem. What should I know about references in order to have a criteria about when to use pointers or references to solve a problem? Thanks for answering. – loguepw Aug 30 '14 at 19:41