int s = myfunction( a, b, c, d);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}
o = f(i1, i2)
(o1, o2) = f(i1, i2)
(int o1, bool o2) f (float i1, float i2)
{
// whatever
}
tester( &x, &y ); // will return x and y to the caller.In C++ and C#, we can return a single object (but not multiple objects). I think we are still talking about the same category as a C struct.
#define IN#define OUT
tester(OUT &x, OUT &y ); // will return x and y to the caller.
(x2, y2, z2) = f (x1, y1, z1, 5); // f is a functionand(s, t) = (25, t + 42); // sets s = 25 and t = t + 42andF1 ( F2 (5 , 7.2), "some string"); // F1 and F2 are functions. F2 returns one or more values which F1 uses for inputs
Wow! Right on explanations from Dan and Brad.
If internally C would use a separate data stack, then I
would think passing multiple values to and from functions would be natural. But
since C uses a register for returning values. Using a register would take less
CPU than a stack. And I think optimizing C compilers would use a register when
possible to pass an argument to the function.
These days, we are concerned with multiple CPUs. Maybe we block simultaneous execution of a function by multiple CPUs, or we write re-entrant functions that can be executed by multiple CPUs simultaneously. Seems to me that this can effect how we implement multiple return values.
When you call a function for which you need to check for success and error, multiple return values would be convenient. For example, open_file(). You get a file handle returned. If the file handle is invalid (maybe use zero for an invalid handle), then you need to call some other function to find out why. With multiple CPUs, another CPU could execute open_file() before you get a chance to call the error function to find out why the file did not open. Maybe we use try-catch. But is try-catch simpler than returning multiple values? A moot point.
IPs are multi-value. What would be the most FBP-way for open_file()? Or maybe FBP would relegate to the component. If error, the open_file component sends one message to component A; if success, send a different message to component B?
In C's case, the reason is more that it pushes incoming arguments onto a stack (i1, i2) but returns outgoing stuff via explicit code, typically in a register. Single value lives in that register when it unwinds the call stack. Multiple value returns are hard in C because there's no place to reliably store return values. Thus the requirement to pass &struct as an argument to return struct as a value.
Ron, it seems to me that you are conflating several different ideas. Returning multiple values from a function may just be a matter of syntax, ...
...