Friday, July 31, 2009

C programming: Returning multiple arguments from a function.?

In C, how do you return multiple arguments from a function? I'm pretty sure it needs pointers... But I have no clue how!





Please go easy on the jargon, I'm just learning!

C programming: Returning multiple arguments from a function.?
Yeah you got it right. You have to use the pointers for that purpose.


If you have to return the values that are of the same data type (eg. int) then you can use array to return multiple values.


But if you need to return values of different data types, then you have to use the pointers (in C).


for example: this function will put values in a, b and c and return them all.


void ReturnVals(int %26amp;a, int %26amp;b, int %26amp;c)


{


a=2;


b=3;


c=4;


}





call this function from somewhere else using the code like:





int val1,val2,val3;


ReturnVals(val1, val2, val3);


printf("%d, %d, %d",val1,val2,val3);





Then you'll get the output as 2,3,4


As you've seen we've succeeded in getting multiple return values from the function ReturnVals.
Reply:You can't return more than one argument from a function call. However, there are ways around this. The easiest way is to create a structure and then pass the address of the structure as one of the arguments to the function call. The function can then assign values to the various members of the structure and they will still be intact when the function exits. I hope this isn't too heavy on jargon. Here's an example: (but please forgive any syntax errors - it's been years since I did C programming...)





main()


{


struct


{


int a;


char b ;


} myStruct ;





DoFunc (%26amp;myStruct) ;


printf ("a=%n, b=%c\n", myStruct.a, myStruct.b) ;


}





void DoFunc (myStruct *TheStruct)


{


TheStruct.a = 2;


TheStruct.b = 'q' ;


}





A should be equal to 2 and b should be equal to q.
Reply:when defining the function, put multiple arguments in the brackets.





main(a, b, c)





instead of





main(void)





Hang on, that might be arguments supplied _TO_ the function, err, sorry, I'm learning too.





Good luck.
Reply:The only way to return multiple arguments is to return a struct. It is not obligatory to use pointers. You have to define a struct with all the information you need to return


typedef struct sth { int a;


int b;


char c;}sth;





sth func(int aa,int bb,char cc)


{ sth my_sth;





my_sth.a=aa;


my_sth.b=bb;


my_sth.c=cc;





return my_sth;


}





main()


{ sth sth_ret;


int a1=1,b1=2;


char c1='a';





sth_ret=func(a1,b1,c1);





printf("%d %d %c\n",sth_ret.a,sth_ret.b,sth_ret.c);


}
Reply:Here's the "complete" way to do it.





#include%26lt;iostream%26gt;





using std::cin;


using std::cout;


using std::endl;








void Return2variables(int %26amp;variable1, int %26amp;variable2);





int main()


{


int variable1;


int variable2;





cout %26lt;%26lt; "Enter a variable: ";


cin %26gt;%26gt; variable1;





cout %26lt;%26lt; "Enter another variable: ";


cin %26gt;%26gt; variable2;





Return2variables(variable1, variable2);





cout %26lt;%26lt; endl %26lt;%26lt; endl;


cout %26lt;%26lt; "You have exited the function and now you're in main function" %26lt;%26lt; endl;


cout %26lt;%26lt; "Here are the variables after they got converted: ";


cout %26lt;%26lt; variable1 %26lt;%26lt; "," %26lt;%26lt; variable2 %26lt;%26lt; endl;





system("PAUSE");


return 0;


}





void Return2variables(int %26amp;variable1, int %26amp;variable2)


{


cout %26lt;%26lt; "You're in the function" %26lt;%26lt; endl;


cout %26lt;%26lt; "These are the variables before they get converted: ";


cout %26lt;%26lt; variable1 %26lt;%26lt; "," %26lt;%26lt; variable2 %26lt;%26lt; "," %26lt;%26lt; endl;





variable1 = 17;


variable2 = 18;





}
Reply:I don't really use C, but you could probably do this by returning an array or some type of object.
Reply:All the answerers here are terrible. Some of you give misguided answers. The code many of you write is flawed. If you don't know C, don't write a wrong answer. Please stop. It's one thing to be a poor programmer yourself, don't make others bad.





http://c-faq.com/misc/multretval.html





A structure is probably easiest. If you're using pointers, have pointers passed in through the function arguments, and then modify the values pointed to by the pointers.





Be careful of returning pointers. If you declare a variable on the stack, pointers to it won't be valid after the function returns. So if you're returning pointers, make sure they point to memory on the heap.


No comments:

Post a Comment