Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning multiple values from a C function

Important: Please see this very much related question: Return multiple values in C++.

I'm after how to do the same thing in ANSI C? Would you use a struct or pass the addresses of the params in the function? I'm after extremely efficient (fast) code (time and space), even at the cost of readability.

EDIT: Thanks for all the answers. Ok, I think I owe some explanation: I'm writing this book about a certain subset of algorithms for a particular domain. I have set myself the quite arbitrary goal of making the most efficient (time and space) implementations for all my algos to put up on the web, at the cost of readability and other stuff. That is in part the nature of my (general) question.

Answer: I hope I get this straight, from (possibly) fastest to more common-sensical (all of this a priori, i.e. without testing):

  1. Store outvalues in global object (I would assume something like outvals[2]?), or
  2. Pass outvalues as params in the function (foo(int in, int *out1, int *out2)), or
  3. return a struct with both outvals, or
  4. (3) only if the values are semantically related.

Does this make sense? If so, I think Jason's response is the closest, even though they all provide some piece of the "puzzle". Robert's is fine, but at this time semantics is not what I'm after (although his advice is duly noted)

Thank you all.

like image 357
Dervin Thunk Avatar asked Jul 06 '09 18:07

Dervin Thunk


People also ask

Can I make a function return multiple values?

You can return multiple values from a function using either a dictionary, a tuple, or a list. These data types all let you store multiple values.

How do I return multiple values from a return statement?

No, you can not return multiple values like this in C. A function can have at most one single return value.

How many values can a function return in C?

A function in C can be called either with arguments or without arguments. Always, Only one value can be returned from a function. If you try to return more than one value from a function, only one value will be returned that appears at the rightmost place of the return statement.


2 Answers

Both ways are valid, certianly, but I would would consider the semantics (struct vs parameter reference) to decide which way best communicates you intentions to the programmer.

If the values you are returning are tightly coupled, then it is okay to return them as a structure. But, if you are simply creating artificial mechanism to return values together (as a struct), then you should use a parameter reference (i.e. pass the address of the variables) to return the values back to the calling function.

like image 51
Robert Cartaino Avatar answered Sep 21 '22 17:09

Robert Cartaino


As Neil says, you need to judge it for yourself.

To avoid the cost of passing anything, use a global. Next best is a single structure passed by pointer/reference. After that are individual pointer/reference params.

However, if you have to pack data into the structure and then read it back out after the call, you may be better off passing individual parameters.

If you're not sure, just write a bit of quick test code using both approaches, execute each a few hundred thousand times, and time them to see which is best.

like image 33
Jason Williams Avatar answered Sep 20 '22 17:09

Jason Williams