-6

I'm a beginner in C. I heard that a function in C cannot return more than one value, but this is not the case in my program; it's returning more than one value and the code is running fine. Please explain.

#include<stdio.h>
int main () {
    int a=10,b=9;
    return a,b;
}
karel
  • 5,489
  • 46
  • 45
  • 50

7 Answers7

3

To answer the question: Not that way, no.

  • You could return a pointer to a malloc'd array containing multiple values.

  • Or return a pointer to a malloc'd struct containing multiple values

  • Or you could pass in pointers to storage locations as part of the arguments to this function, and set the values there.

  • Or you could shove some of the values into existing data structures, dynamic or static, that this function already has access to.

  • Or you could divide up an int or long into bitfields which carry the several values.

  • ... There are probably other solutions.

Of course for any of these to work, the caller has to know what approach you're using and invoke the function properly.

But in C, the comma is NOT a solution for returning multiple values. The comma operator evaluates its two operands, discards the result of the first, and returns the second. So your sample is just looking at a, throwing that away, and returning b. Definitely not what you thought you were doing.

(And I should point out that C uses comma for several different purposes; the comma here is NOT the same as comma in an array initializer, for example.)

For what it's worth, I have used one language that permitted multi-value expressions separated by commas: CLU, which was an ancestor of a lot of the object-oriented languages. In CLU you could write statements like a,b=b,a to exchange two values, or `r,theta=cartesianToPolar(x,y)" to do coordinate conversion. That was a nice feature, and easy to implement on modern stack machines.

(CLU had a number of other nice features that weren't picked up by later languages; I've always meant to go back and try to find out why not.)

keshlam
  • 7,931
  • 2
  • 19
  • 33
1

no you cant return multiple value from a function , instead you can return an array to return multiple values of same datatype or you can return a structure to return multiple values of different datatypes

varun
  • 1,473
  • 1
  • 9
  • 15
1

Function can return a single value .

 #include<stdio.h>
 int main () {
  int a=10,b=9;
  return a,b;
 }

when your returning like that. here comma (,). operator will work. it will return the last value from the list.

Bhuvanesh
  • 1,269
  • 1
  • 15
  • 25
0

You asked:

Can a Function return more than 1 value in C

You can do that if you return a struct but not from main. main can only return an int.

typedef struct MyStruct {int a; int b;} MyStruct;

MyStruct foo(int a, int b)
{
    return MyStruct{a, b};  
}

int main ()
{
   int a=10,b=9;
   MyStruct s = foo(a, b);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

No. A return statement (§6.8.6.4) is only allowed one expression or none at all. An expression is defined as:

§6.5

An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. The value computations of the operands of an operator are sequenced before the value computation of the result of the operator.

Since an object has a "value", you cannot return more than one value.

What you're encountering is known as the comma operator (§6.5.17), where the left operand of the operator is evaluated as a void expression (basically means it's discarded) and yields the right operand. So in your case, a is discarded and b is yielded.

0

A function can return only one value. Here in your code it work like this.

int a=(1,10);

Now you print the value of the a it will be 10. So like this while giving the comma(,) in return it works. So if you check the return value of your code it will be nine.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
0

Don't see with the programming eyes only. the word function in computer programming hails from mathematics, we could have all experienced with famous function in maths f(x,y..)= x(.)y..(what ever computation). that is at last of any complex computation is result in a single value obviously that is function return value.

and now if you want function sending too many values bind all values into a structure as a single value then return.