| | |
func return 2 values
![]() |
•
•
Join Date: Jan 2009
Posts: 36
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
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.
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.
You can't return two values from a functio using the return statement. Use a struct as you've been told to above:
C Syntax (Toggle Plain Text)
typedef struct { int x, y; } P; P getPoint() { int a, b; //a = .... b = ..... P temp; temp.x = a; temp.y = b; return temp; }
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
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;
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".
The approach with using pointers is;
C Syntax (Toggle Plain Text)
SomeTypeA Function( <arguments>, SomeTypeB *x) { *x = whatever_data_needed_other_than_return_value(); return return_value(); }
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:
C Syntax (Toggle Plain Text)
char arithmeticOperation(int a, int b, int c) { // + if(a + b == c) return '+'; // - if(a - b == c) return '-'; //you also do this if the order you pass the numbers isn't critical if(b - a == c) return '-'; // * if(a * b == c) return '*'; // / if(a / b == c) return '/'; if(b / a == c) return '/'; // % if(a % b == c) return '%'; if(b % a == c) return '%'; //if nothing matches return ' '; // return a space } int main(void) { char c = arithmeticOperation(4, 2, 0); if(c == ' ') // check if it's a space which means there isn't an operation printf("No arithmetic operation found.\n"); else printf(%c, "Operation ", c, " found.\n"); }
Last edited by minas1; Jan 10th, 2009 at 7:12 am.
•
•
Join Date: Jan 2009
Posts: 36
Reputation:
Solved Threads: 0
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 !!!
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 !!!
![]() |
Similar Threads
- How do I recognise strings and numbers (Python)
- How to create a .lib from a .def? (C++)
- Any help is appreciated (C++)
- so I find myself here again (C++)
- Return more than one value from a single function (C++)
Other Threads in the C Forum
- Previous Thread: Thread Scheduling - Please Help
- Next Thread: An implementation idea requied in Compiler Interface
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char cm convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory fflush file fork forloop frequency givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram homework i/o inches infiniteloop input interest iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number open opendocumentformat owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h windowsapi






