0

if possible then how to return more than one value. It can not be because of return is done through Accumulator in controller or CPU.

Is this is the correct reason?

kapilddit
  • 1,729
  • 4
  • 26
  • 51
  • Why there is restriction on returning a number of values? – kapilddit Jun 08 '12 at 13:44
  • What would it even mean to return more than one value? – Daniel Fischer Jun 08 '12 at 13:44
  • If you have to return a range of values, a pointer type would help; if you have to return two differently typed values (int and char), you'd need two different functions. – Makoto Jun 08 '12 at 13:45
  • Because the syntax wouldn't make any sense. Try writing code that would use such a feature. – SLaks Jun 08 '12 at 13:45
  • possible duplicate of [returning multiple values from a function](http://stackoverflow.com/questions/3829167/returning-multiple-values-from-a-function) – huon Jun 08 '12 at 13:45
  • PL/M is a language that is able to return more than one value. – jacknad Jun 08 '12 at 13:46
  • As we can pass number of variables to function, and variable will create space in stack. then why not we can return multiple variable. if its return from stack or some memory? – kapilddit Jun 08 '12 at 13:52
  • 1
    Suggested rewrite of the question, assuming that I grasp your meaning correctly: "Why can one return no more than one value from a function in C? Is it because the controller or CPU passes return values back to the caller via its accumulator?" – thb Jun 08 '12 at 13:55
  • @thb yes, you grasp it correctly. – kapilddit Jun 08 '12 at 13:58
  • 1
    By the way, it is *not* the case that all function return values are returned in the same register. The standard has nothing to say about calling conventions, but implementations sometimes use different registers or stack locations for different types of return value. A function that returns `double` doesn't necessarily return it the same way as a function that returns `int`, and functions that return large structs clearly cannot cram all that data into a register. Caller and callee both know what the return type is, so they just have to agree how that type is returned, as defined in an ABI. – Steve Jessop Jun 08 '12 at 14:11

4 Answers4

9

Because that's how the language is defined. There is no fundamental reason; other languages can return multiple values (e.g. Matlab).

As a "workaround", you can return a struct that contains multiple fields.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • You can also pass in a pointer and modify it from your function. – David B Jun 08 '12 at 13:46
  • As we can pass number of variables to function, and variable will create space in stack. then why not we can return multiple variable. if its return from stack or some memory? – kapilddit Jun 08 '12 at 13:54
  • 2
    @kapilddit: Because that's the language is defined. – Oliver Charlesworth Jun 08 '12 at 13:55
  • 5
    Closest I can think to a "fundamental" reason is that an expression in C has a value (or is void), not multiple values. So, for a function call to be an expression, the function has to return a value (or void), not multiple values. It's nothing to do with CPU registers, the stack, or memory. That could all be sorted out if the language designers had wanted to do things differently. – Steve Jessop Jun 08 '12 at 13:59
3

You can totally "return" more than 1 item by putting them into a struct, or pass a pointer to the function so that the function can write some value that persists after the function has returned.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
1

You cannot return more than one value at a time. because the syntax for returning a function is defined in such a way that it should accept only one value. check the below example.

a=returnfunction();

In the above case if the function returnfunction() returns more than one value, then the compiler gets confused to initialize which returned value to the variable a. if the function returnfunction() returns two values 1 and 2, then the compiler get confused of assigning the value 1 or 2 to the variable a. so in order to prevent these type of problems the functions are defined in such a way to not to return more than one value.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
0

Contrary to how it's expressed, no value actually gets "returned". In fact the return value/address is passed as an argument on stack to the function call. Then the return statement actually modifies that argument, thus passing result to the caller.

In some sense, it's almost like void func(arg1, arg2, *ret)

Different languages handle this differently. C expects only one "return" argument on stack. Indeed structure will let you "return" multiple values, so would passing a pointer to a structure as a normal argument do as well.

vmsnomad
  • 319
  • 2
  • 7