943,867 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2098
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 10th, 2009
0

func return 2 values

Expand Post »
Hello,

I have a question, How I can write a function that return 2 values.
the function should take 3 numbers and do between the first and the second number an arethmetic operation and check if the result will be the third number, if yes the function return 1 and return the character of the arethmetic operation.
for example: the function take the numbers 3 5 15, the function will check the three numbers, and will fint that 3*5= 15, so it will return 1 and return *.

I hope that you understand me, and I hope that you can help me with the first thread
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
titosd is offline Offline
56 posts
since Jan 2009
Jan 10th, 2009
0

Re: func return 2 values

A common way of return multiple values is by returning a struct which has multiple members, or for the function to accept a pointer that receives additional data from the function.

You don't actually need to return multiple values though: return 0 if there is no operation, '*' if multiplication is the operation, '+' if addition, etc etc. '*', '+', '-', and '/' are non-zero characters with all character sets that I know of.
Last edited by grumpier; Jan 10th, 2009 at 6:10 am.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 10th, 2009
0

Re: func return 2 values

thank you grampier
but by the question that i have I should reurn 2 values if there is operation return 1 and the character, I asked somebody the all tell me that I should use the pointers but anyone of them know how should write the function.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
titosd is offline Offline
56 posts
since Jan 2009
Jan 10th, 2009
0

Re: func return 2 values

You can't return two values from a functio using the return statement. Use a struct as you've been told to above:

  1. typedef struct
  2. {
  3. int x, y;
  4. } P;
  5.  
  6. P getPoint()
  7. {
  8. int a, b;
  9. //a = .... b = .....
  10. P temp;
  11. temp.x = a;
  12. temp.y = b;
  13. return temp;
  14. }
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Jan 10th, 2009
0

Re: func return 2 values

thanks minas
but I want use the poiner, I sure that I can use the pointer, but I didn't know how to use it !!
is anyone know ?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
titosd is offline Offline
56 posts
since Jan 2009
Jan 10th, 2009
0

Re: func return 2 values

You need to put some effort into understanding the answers you receive, rather than giving up if you're not given code examples.

The approach with using pointers is;
  1. SomeTypeA Function( <arguments>, SomeTypeB *x)
  2. {
  3. *x = whatever_data_needed_other_than_return_value();
  4. return return_value();
  5. }
The "SomeTypeB *x" argument is what I was referring to when I said "or the function to accept a pointer that receives additional data from the function".
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 10th, 2009
0

Re: func return 2 values

So 1 has to be returned for you to know that the result is the third number. You can do it with only one return value:

  1. char arithmeticOperation(int a, int b, int c)
  2. {
  3. // +
  4. if(a + b == c) return '+';
  5.  
  6. // -
  7. if(a - b == c) return '-';
  8.  
  9. //you also do this if the order you pass the numbers isn't critical
  10. if(b - a == c) return '-';
  11.  
  12. // *
  13. if(a * b == c) return '*';
  14.  
  15. // /
  16. if(a / b == c) return '/';
  17. if(b / a == c) return '/';
  18.  
  19. // %
  20. if(a % b == c) return '%';
  21. if(b % a == c) return '%';
  22.  
  23. //if nothing matches
  24. return ' '; // return a space
  25. }
  26.  
  27. int main(void)
  28. {
  29. char c = arithmeticOperation(4, 2, 0);
  30.  
  31. if(c == ' ') // check if it's a space which means there isn't an operation
  32. printf("No arithmetic operation found.\n");
  33. else
  34. printf(%c, "Operation ", c, " found.\n");
  35. }
Last edited by minas1; Jan 10th, 2009 at 7:12 am.
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Jan 10th, 2009
0

Re: func return 2 values

thank you grumpier
but I didn't understand the example, if you can give me actual example plz.

minas thank you, but I said that we should write the function with pointers, and te function should return 2 values (the character and the number 1).

I can write it by the structer but we should write it just with pointers !!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
titosd is offline Offline
56 posts
since Jan 2009
Jan 10th, 2009
0

Re: func return 2 values

Do you want the function to take pointers as parameters or return a pointer or both?
And why do you want to return 1?
Last edited by minas1; Jan 10th, 2009 at 7:26 am.
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Jan 10th, 2009
0

Re: func return 2 values

Click to Expand / Collapse  Quote originally posted by titosd ...
but I didn't understand the example, if you can give me actual example plz.
I can, but I won't. As horrifying as the concept clearly is, you need to apply some effort to understand the answers you've been given.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

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: Thread Scheduling - Please Help
Next Thread in C Forum Timeline: An implementation idea requied in Compiler Interface





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


Follow us on Twitter


© 2011 DaniWeb® LLC