how many ways can you pass parameters to a function in c++

how many ways can you pass parameters to a function in c++

Do you mean this?

  1. By value.
  2. By pointer.
  3. By reference.

>1. By value.
Okay.

>2. By pointer.
Pointers are passed by value, let's not add to the massive amount of confusion surrounding pointers, k? ;)

>3. By reference.
Okay.

You can also pass values and types to a function by way of templates. But the original question is rather vague, so it might be best to wait for clarification before answering definitively.

>2. By pointer.
Pointers are passed by value

2. Using a pointer to simulate pass by reference.

let's not add to the massive amount of confusion surrounding pointers, k?

Too late(?)

>2. Using a pointer to simulate pass by reference.
Much better. ;)

>Too late(?)
Better late than never.

Allow me to make things more confusing....

Narue, when you say pointers are passed by value, I don't understand what you mean.

void foo ( int byVal, int *byPointer, int &byRef );

the second integer parameter is passed by pointer.

I'm guessing you are referring to passing a pointer by value in some specific case? But you can replace <int> above with anything including pointers, and in that case you could have something like this:

void Bar( char* s1, char** s2, char*& s3 );

only in the first case is the pointer passed by value in the sense you mean, I'd guess? But it depends on your point of view: It could be a pointer to a char or it could be a char pointer you are passing by value!

I'm not disagreeing with you, but I don't see how that modifies Dave's original list of by value, by pointer, or by reference.

>Narue, when you say pointers are passed by value, I don't understand what you mean.
The pointer is passed by value, even if you can get to the original object by dereferencing it. This is a poor man's call-by-reference because you aren't actually calling by reference, just passing an address so that you can fake the behavior of call-by-reference.

I admit that it's a subtle difference, but trust me that it's an important one to understand completely. :)

Ah, and here all this time I've been thinking of pass by reference as the same as pass by pointer, but with the compiler hiding the pointer syntax from you. :0)

Yeah, bottom line is that the stack only holds (on Windows) 32-bit values, and the compiler can emit an int, a padded char or short (padded to 32 bits), or an address of something. Or, I suppose, two 32 bit values for 'double' and 64-bit values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.