I want a function to return multiple values after doing data processing i.e. void test(int a, int b int c) whereas a, b and c have values set and control is returned to the caller with the values sent back. Try as I may I haven't been able to return values from my function, furthermore I read from someone C doesn't support multiple returns - is this correct??
It seems such a stupid limitation when (supposedly) inferior programming languages like Pascal can support multiple returns in a function / procedure call.
What gives??
Is this just in Keil?? Can I get it to do what I want??
I really want to avoid using global declarations, records or structs if at all possible.
Heaven forbid you read the C Language documentation.
Return a structure, a static one if you want, or pass in a structure, and modify the values there in. Pass as pointers.
C is a systems programming language, PASCAL is more for academics, they are polished differently to suit.
Westonsupermare Pier BEng (Hons)
"I read from someone C doesn't support multiple returns"
"Someone" being none other than Messrs Kernighan & Ritchie - the authors of the language?
"is this correct?"
Yes. Any decent textbook should be clear on this.
"Is this just in Keil?"
No.
"I really want to avoid using ... structs if at all possible."
Why? That would be the usual way to do it.
Or pass the parameters by reference (pointer)?
Even farmers do these days.
Old Mac Donald. EI, EIO.
It seemed appropriate in the context, I'll put it back in my pants now.
www.youtube.com/watch
It seems u dont know the 'pass by reference' method of passing the variables. You may not even want to return multiple values if you know the advantages of pass by reference technique.
denniskubes.com/.../
The "traditional" route, for people who don't see limitations but possibilities with the programming language, would probably look like:
int my_function(my_struct* result) { if (!result) { return RES_NAUGHTY_TRICKSTER; } result->charlie = 1.6180339887; result->benny = 2.7182818; result->johnny = 3.14159265; return RES_OK; }
So the "return" is if the function could perform or not, while the struct gets the result of the computation - but only valid if the function returned an OK.
Use lists or arrays. The array is my favourite.
If you're coding in C there are many many ways to do this. Give it a google.
Also try to use more gotos. They're underrated.
for some people who love goto: goto: - the 1st time in my life i m using goto with some thing related to C :P (punintended) http://www.keil.com/forum/12771/
There are many more threads on this forum. you can just read them, before using goto.
PS: I ain't no against it. But till date haven't found any purpose of using it. Neither have stumbled across a single circumstance where i could not code without using goto.
And of course, the goto is especially when making a nice software delay loop.
As mentioned in that link, their use breaks the amount of optimization the compiler can carry out, so obviously code execution times becomes so much more predictable.