954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

parameters

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

vudatalark
Newbie Poster
1 post since Sep 2004
Reputation Points: 11
Solved Threads: 0
 
how many ways can you pass parameters to a function in c++

Do you mean this?By value.
By pointer.
By reference.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>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(?)

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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

>Too late(?)
Better late than never.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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 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.

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

>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. :)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You