Parameter Passing

Pages: 12
Hi all..
i have another question.
I got this question in my university exam.
List three ways of passing a parameter (or return value) in C++.

there are 2 ways as far as i know,
pass by value and pass by reference.
is there any third way of passing?

Thank You :)
pass by pointers
i am posting an external link..
http://www.codeguru.com/forum/showthread.php?t=343473
it states here that passing by pointer does not exist.
it is a misconception.
Ok then I don't know what is the third way already. Maybe someone else can help you.
I wouldn't be surprised if they did mean pass-by-pointer; there are a number of different opinions on whether that counts as pass-by-value, pass-by-reference, or some weird hybrid.

Strictly speaking, the pointer object is copied and the receiving function gets a copy of it to do what it likes with, which is undeniably pass-by-value. The fact that one can carry out a dereference operation on the pointer, and in doing so manipulate an object without having had a copy of it passed in, leads many people to think of passing by pointers as a kind of hybrid pass-by-reference.

My take is that passing a pointer is a pass-by-value and that's all there is to it in terms of pass-by-value or pass-by-reference, but other opinions differ.

closed account (z05DSL3A)
List three ways of passing a parameter

The term 'ways of passing' seems a bit misleading. You don't 'pass' parameters, they are a definition. You can define a Parameter to be a:
1. Nonreference Parameters
2. Pointer Parameters
3. Reference Parameters

When an argument is assigned to a parameter, either a copy is made of the argument (pass by value) or a reference is made (pass by reference).
Last edited on
@all
Thank you for replying :)

i guess my university takes pass by pointer as a "way of passing", so for the time being, to get my marks i need to write pass by pointer as the third one :p

but i also think that pass by pointer is same as pass by value.

thank you :)
cilu is probably one of the few people in the world that make such a semantic argument. I am certain that
pass-by-pointer is the answer you are looking for.
Thank you, Js
closed account (z05DSL3A)
This has been bouncing round in the back of my head all day.

Out of curiosity, is there any formal definition of pass-by-value and pass-by-reference? I can't think of when I first came across the terms so don't know if they are just phrases that are used or if they have defined meanings. I also know them by call-by-X and they are evaluation stratagies i.e for call-by-value, the argument operand in evaluated to a value and copied to the parameter.
Last edited on
I tend towards one supreme authority and one lesser authority in C++; the ISO spec and Stroustrup's "The C++ Programming Language".

In this case, Stroustrup (3rd Edition, which is the one I have on my desk) section 7.2 makes it clear what is meant by (although does not explicitly define) the two. I don't have my copy of the ISO standard to hand.

Within C++, I'd tend to go with the meanings stated there (assuming the ISO standard agrees with Stroustrup, which I hope is the case).

Outside of C++, I expect it's open for debate :)
closed account (z05DSL3A)
I will be checking my copy of Stroustrup later, it is at home. I did a search in the standard but only came up with one reference to pass-by-value (no definition).

Sorry for this busymaverick. I'm sure that the answer they want is pointers, I just think the question is 'off'.
there us nothing to be sorry for wolf :)

i tend to think that passing by pointer will be a case of pass by value.
coz we pass the value of the pointer to the function and use it there.

Thanks all for replying :)

Regards,
Sworoop Mahapatra
Undergraduate
i tend to think that passing by pointer will be a case of pass by value.
coz we pass the value of the pointer to the function and use it there.

This came up yesterday. I think you're wrong and here's why: http://www.cplusplus.com/forum/beginner/35219/#msg190580

You can play with words and say you're passing the pointer by value, but as I said there, you're really passing an object by reference, because a pointer isn't interesting by itself. It's just some data that points to the object of interest, a way to refer to some other object.
You can play with words and say you're passing the pointer by value,



If I have a function whose purpose is to carry out pointer arithmetic on a pointer I pass to it by value, that's clearly pass-by-value.

If I pass an uninitialised pointer (or a null pointer) to some function, there is no object it points to, so that clearly can't be pass-by-reference.

If I have a class that holds a hash table of pointers, acting as a storage class for pointers to allocated objects (a class I actually have in production code at the moment), I pass in the pointer by value and the class stores that copy of it so that I can retrieve a copy of the pointer later when I need it. That's all about the pointer and it's pass-by value.

It seems to me that you are the one playing with words, saying that in one case it's pass-by-reference, and in other cases (such as these examples) it's pass-by-value, when it's the exact same operation being undertaken.
Last edited on
At the low-level, pass by pointer is exactly the same as pass by reference. They just have different syntax, and references cannot be null.
Last edited on
If I have a function whose purpose is to carry out pointer arithmetic on a pointer I pass to it by value, that's clearly pass-by-value.

In that case, I'm inclined to agree.

If I pass an uninitialised pointer (or a null pointer) to some function, there is no object it points to, so that clearly can't be pass-by-reference.

Here, I disagree. Passing an uninitialised pointer is an error. And the pointer is still referring to some random address. Passing a null pointer only makes (some) sense if the function is supposed to have it point at some object you can use after the call. So you're sending in a reference to nothing and expecting the function to fill it with a reference to something.

It seems to me that you are the one playing with words, saying that in one case it's pass-by-reference, and in other cases it's pass-by-value, when it's the exact same operation being undertaken.

If we follow your reasoning all the way, nothing is ever passed by reference, since a reference is itself an object (it just happens to always point to the same thing). To make things clear, consider:

1
2
3
4
5
6
7
8
9
10
void foo(obj* o)
{
    // code
}

int main()
{
    obj bar;
    foo(&bar);
}

Would you really talk about that function by saying you pass the parameter by value? The immediate assumption would be that a copy of bar is made and bar can't be directly accessed from within foo().

Actually, I think the best term would be pass-by-pointer, after all.
Hi Rocket, I think everyone knows already that you and I disagree on this one, so I'll not rehash it again! :)
If we follow your reasoning all the way, nothing is ever passed by reference, since a reference is itself an object (it just happens to always point to the same thing).


By my reasoning, which I infer from section 7.2 of Stroustrup, if a copy of the object is made and passed to the receiving function (and thus the receiving function can do whatever it likes to the object and, upon the return to the calling scope, the original object that was copied is unchanged), it's pass by value.

If messing around with the object (not dereferencing it and messing around with a different object) in the function changes the object in the calling scope, it's pass by reference.

Would you really talk about that function by saying you pass the parameter by value? The immediate assumption would be that a copy of bar is made and bar can't be directly accessed from within foo().


I would say that is pass-by-value. If someone were to assume that a copy of bar is made, they've misread the code.

If we follow your reasoning all the way, nothing is ever passed by reference, since a reference is itself an object (it just happens to always point to the same thing).


No, that is not what my reasoning says. My reasoning is that if a copy is made, and changes to that copy do not affect the original in the calling scope, it is pass by value. When you pass by reference, the receiver does not have a copy to play with; changing the passed object changes the original in the calling scope.


As an aside, I can think of occasions when passing a null or an uninitialised pointer would not be any kind of error. In fact, the circumstances you thought of that would justify sending in a null pointer apply equally to sending in an uninitialised pointer!

As another example, where an uninitialised pointer would be an error, I have some code that will test the provided pointer, and if it's null, not carry out an operation (the pointer gets set to an output stream by another class, and I like these classes to take care of themselves, so they do their own testing on provided objects; I could do the testing before calling the class function, but as I said, in this case I like the objects to take care of themselves as much as possible).

Last edited on
Good grief! What a discussion.

The answers are, as already given:

  pass by value - as the object itself (or a copy of it)
  pass by pointer - as the address of the object itself (an indirect reference)
  pass by reference - as an alias for the object itself (a direct reference)

Whether or not you can quibble about the words used or what may or may not happen using the various methods, the industry does have existing jargon, which, in order to promote consensus and continuity, we continue to use.

If this were a C forum, and not C++, we would say "pass-by-value and pass-by-reference" because we would mean "pass-by-value and pass-by-indirect-reference, AKA pass-by-pointer."
Pages: 12