func return 2 values

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 36
Reputation: titosd is an unknown quantity at this point 
Solved Threads: 0
titosd titosd is offline Offline
Light Poster

func return 2 values

 
0
  #1
Jan 10th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: func return 2 values

 
0
  #2
Jan 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 36
Reputation: titosd is an unknown quantity at this point 
Solved Threads: 0
titosd titosd is offline Offline
Light Poster

Re: func return 2 values

 
0
  #3
Jan 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: func return 2 values

 
0
  #4
Jan 10th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 36
Reputation: titosd is an unknown quantity at this point 
Solved Threads: 0
titosd titosd is offline Offline
Light Poster

Re: func return 2 values

 
0
  #5
Jan 10th, 2009
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 ?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: func return 2 values

 
0
  #6
Jan 10th, 2009
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".
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: func return 2 values

 
0
  #7
Jan 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 36
Reputation: titosd is an unknown quantity at this point 
Solved Threads: 0
titosd titosd is offline Offline
Light Poster

Re: func return 2 values

 
0
  #8
Jan 10th, 2009
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 !!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: func return 2 values

 
0
  #9
Jan 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: func return 2 values

 
0
  #10
Jan 10th, 2009
Originally Posted by titosd View Post
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.
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