Can a function return 2 values?

Reply

Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Can a function return 2 values?

 
0
  #1
Jan 21st, 2007
I have one or more functions separate from main() , that besides the return value , must also return the time it tooked to process.
How can I do that ?
"Get rich or die tryin"(50-cent)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,374
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: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Can a function return 2 values?

 
0
  #2
Jan 21st, 2007
make a parameter that is a pointer to the object you want the function to return Example:
  1. int foo( size_t* time)
  2. {
  3. *time = 123;
  4. return 1;
  5. }
  6.  
  7. int main()
  8. {
  9. size_t processing_time;
  10. int x = foo( &processing_time );
  11. }

If you want to profile the entire function then the function doesn't need to know anything about it.
  1. int main()
  2. {
  3. time_t start, end;
  4. start = time(0);
  5. foo();
  6. end = time(0);
  7. // now just subtract the two times to get processing time
  8.  
  9. return 0l;
  10. }
Last edited by Ancient Dragon; Jan 21st, 2007 at 8:49 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Re: Can a function return 2 values?

 
0
  #3
Jan 21st, 2007
After you said , at the first method ,i came up with this:
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4.  
  5. int foo( clock_t* time)
  6. {
  7. int x=0;
  8. while(x<1000000000)
  9. x++;
  10. *time=clock()/ (long) CLK_TCK;
  11. return x;
  12. }
  13.  
  14. int main()
  15. {
  16. clock_t processing_time;
  17. int x = foo( &processing_time );
  18. printf("%d\n x value is %d= ",processing_time,x);
  19.  
  20. return 0;
  21. }
I don't know if it's the greatest solution , but it works(i quess),only that isn't very accuarate .How can i make it display small time values(like 0.001 seconds)

The second method doesn't work
Last edited by Eko; Jan 21st, 2007 at 9:10 am.
"Get rich or die tryin"(50-cent)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,374
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: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Can a function return 2 values?

 
0
  #4
Jan 21st, 2007
see my second example -- you have to get the time before processing starts and again after, then subtract the two. And the time is in whole numbers. If you want fractions than divide by 100 when printing it.
  1. print ("time = %f\n", (float)processing_time/100.0F);
Last edited by Ancient Dragon; Jan 21st, 2007 at 11:35 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Re: Can a function return 2 values?

 
0
  #5
Jan 21st, 2007
Well for the second example , I get " error C2064: term does not evaluate to a function taking 1 arguments" at the start and end temp(0)
Last edited by Eko; Jan 21st, 2007 at 11:33 am.
"Get rich or die tryin"(50-cent)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,374
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: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Can a function return 2 values?

 
0
  #6
Jan 21st, 2007
did you include time.h? Also, if you are compiling as c++ then change variable end to something else because end is a c++ key word.
Last edited by Ancient Dragon; Jan 21st, 2007 at 11:37 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Re: Can a function return 2 values?

 
0
  #7
Jan 21st, 2007
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <dos.h>
  4.  
  5. int foo()
  6. {
  7. int x=0;
  8. while(x<1000000)
  9. x++;
  10. return x;
  11. }
  12.  
  13. int main()
  14. {
  15. time_t start, end;
  16. int x,time;
  17. start = time(0);
  18. x=foo();
  19. end = time(0);
  20. // now just subtract the two times to get processing time
  21. time=end-start;
  22. printf("%d s: ",time);
  23.  
  24. return 0;
  25. }
"Get rich or die tryin"(50-cent)
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: Can a function return 2 values?

 
0
  #8
Jan 21st, 2007
Originally Posted by Eko View Post
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <dos.h>
  4.  
  5. int foo()
  6. {
  7. int x=0;
  8. while(x<1000000)
  9. x++;
  10. return x;
  11. }
  12.  
  13. int main()
  14. {
  15. time_t start, end;
  16. int x,time;
  17. start = time(0);
  18. x=foo();
  19. end = time(0);
  20. // now just subtract the two times to get processing time
  21. time=end-start;
  22. printf("%d s: ",time);
  23.  
  24. return 0;
  25. }
You've named one of your variables time, and that conflicts with the time() function.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 61
Reputation: Eko is an unknown quantity at this point 
Solved Threads: 1
Eko's Avatar
Eko Eko is offline Offline
Junior Poster in Training

Re: Can a function return 2 values?

 
0
  #9
Jan 21st, 2007
You were right.It was that time variable :cheesy:

Thanks Ancient Dragos,Ravalon
"Get rich or die tryin"(50-cent)
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC