parameters

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 1
Reputation: vudatalark is an unknown quantity at this point 
Solved Threads: 0
vudatalark vudatalark is offline Offline
Newbie Poster

parameters

 
1
  #1
Sep 16th, 2004
how many ways can you pass parameters to a function in c++
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: parameters

 
1
  #2
Sep 16th, 2004
Originally Posted by vudatalark
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: parameters

 
0
  #3
Sep 16th, 2004
>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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: parameters

 
0
  #4
Sep 16th, 2004
Originally Posted by Narue
>2. By pointer.
Pointers are passed by value
2. Using a pointer to simulate pass by reference.

Originally Posted by Narue
let's not add to the massive amount of confusion surrounding pointers, k?
Too late(?)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: parameters

 
1
  #5
Sep 16th, 2004
>2. Using a pointer to simulate pass by reference.
Much better.

>Too late(?)
Better late than never.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: parameters

 
0
  #6
Sep 16th, 2004
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: parameters

 
0
  #7
Sep 16th, 2004
>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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: parameters

 
1
  #8
Sep 16th, 2004
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC