Function return question?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Function return question?

 
0
  #1
Oct 12th, 2008
Say you have a class called example:

  1. class example
  2. {
  3. int one;
  4. int two;
  5. };

and you use this to get numbers from the user:

  1. example number;
  2.  
  3. cout<<"enter numbers"<<endl;
  4. cin>>number.one>>number.two;

and then you call one function to manipulate the numbers and another to do some more manipulation. How do you get those numbers from one function to the other?

  1. function1(number.one, number.two);
  2. function2(number.one, number.two);
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Function return question?

 
0
  #2
Oct 12th, 2008
Here is one way it could be done.
  1. class example
  2. {
  3. public:
  4. int one;
  5. int two;
  6. };
  7.  
  8. int function1(int one, int two)
  9. {
  10. return one * two;
  11. }
  12.  
  13. int function2(int one, int two)
  14. {
  15. return one + two;
  16. }
  17.  
  18. int main()
  19. {
  20. example number;
  21.  
  22. cout<<"enter numbers"<<endl;
  23. cin>>number.one>>number.two;
  24. cout << function1( function2(number.one, number.two), 3);
  25. }
Last edited by Ancient Dragon; Oct 12th, 2008 at 8:01 pm.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: Function return question?

 
0
  #3
Oct 12th, 2008
is there a way to not put in integer 3? And just get number.one and number.two into the next function?
Last edited by JackDurden; Oct 12th, 2008 at 8:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,679
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Function return question?

 
0
  #4
Oct 12th, 2008
You can do it any way you want. You don't even have to nest the two functions like I showed you.
  1. int n = function2(number.one, number.two);
  2. cout << function1( n, number.two);

There are lots of ways to do what you described. Let your imagenation be your guide and try them out with your compiler to see what happens. Practice makes perfect
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 403 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC