943,965 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 15035
  • C RSS
Jan 21st, 2007
0

Can a function return 2 values?

Expand Post »
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 ?
Similar Threads
Eko
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
Eko is offline Offline
60 posts
since Nov 2006
Jan 21st, 2007
0

Re: Can a function return 2 values?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 21st, 2007
0

Re: Can a function return 2 values?

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.
Eko
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
Eko is offline Offline
60 posts
since Nov 2006
Jan 21st, 2007
0

Re: Can a function return 2 values?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 21st, 2007
0

Re: Can a function return 2 values?

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.
Eko
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
Eko is offline Offline
60 posts
since Nov 2006
Jan 21st, 2007
0

Re: Can a function return 2 values?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 21st, 2007
0

Re: Can a function return 2 values?

  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. }
Eko
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
Eko is offline Offline
60 posts
since Nov 2006
Jan 21st, 2007
0

Re: Can a function return 2 values?

Click to Expand / Collapse  Quote originally posted by Eko ...
  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.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Jan 21st, 2007
0

Re: Can a function return 2 values?

You were right.It was that time variable :cheesy:

Thanks Ancient Dragos,Ravalon
Eko
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
Eko is offline Offline
60 posts
since Nov 2006
Dec 28th, 2009
-2

this could be an example for ur q:ESTION

#include<stdio.h>

int incre(int, int);
void main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("incre a %d%d",incre(a,b),incre(b,a));
}
int incre(int x,int y)
{
int temp;
x=temp;
x=y;
y=x;
return x,y;
}
Reputation Points: 2
Solved Threads: 0
Newbie Poster
motogeeeksatyam is offline Offline
1 posts
since Dec 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help me please ASAP.
Next Thread in C Forum Timeline: C program read txt file ..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC