+8  A: 

Yes - have your function return a struct. Or return the values via reference parameters.

struct A {
   int x, y;
   A( int a, int y ) : x(x), y(y) {}
};

A myfun() {
   return A( 0, 42 );    // return  two values
}

or:

void myfun( int & a, int & b ) {
   a = 0;
   b = 42;
}
anon
Though technically that's still one value in C
Nathan Fellman
Is this answering the question, "Can a function modify more than one value?" It can also modify globals if you want to be complete.
Hogan
but still only one value(its address) has been sent and we can access the whole object by copying it into another object.i want to know specifically whether we can assign two values simultaneously.
CadetNumber1
@ashish No addresses involved. But to answer your second question, no you can't.
anon
+9  A: 

No, but you can return a pair or boost::tuple which can contain multiple values.

In addition, you can use references to return multiple values like this:

void MyFunction(int a, int b, int& sum, int& difference);

You would call this function like this:

int result_sum;
int result_difference;
MyFunction(1, 2, result_sum, result_difference);

As Hogan points out, technically this isn't returning multiple variables, however it is a good substitute.

rlbond
Nit pick: "In addition, you can return multiple values like this:" should be "In addition, you can modify multiple values like this:". You can also modify globally scoped variables.
Hogan
+1  A: 

A function can return values in the specified ways:

  • Via return value of any type
  • Via a pointer
  • Via a reference
  • Via setting a global variable (highly not recommended)

If you need a self contained return value, you would typically wrap the types you need in a struct and return an object of that struct by value. If you want to avoid keeping a local copy you would pass in a reference parameter to be modified.

Brian R. Bondy
+2  A: 

In the boost::tuple library, there's a function called tie that simplifies the process of getting information out of a returned tuple. If you had a function that returned a tuple of two doubles and wanted to load those into two local variables x and y, you could assign your function's return value to boost::tie(x, y).

Example:

#include <math.h>
#include <iostream>
#include <boost/tuple/tuple.hpp>

const double PI = 3.14159265;

boost::tuple<double, double> polar_to_rectangular(double radius, double angle)
{
    return boost::make_tuple(radius * cos(angle), radius * sin(angle));
}

int main()
{
    double x;
    double y;

    boost::tie(x, y) = polar_to_rectangular(4, (45 * PI) / 180);
    std::cout << "x == " << x << ", y == " << y << std::endl;

    return 0;
}
Josh Townzen
cool, didnt know that
Viktor Sehr
A: 

main()
{
int a=10,b=20;
int *c;
c=aa(a,b);
printf("%d %d",*c,*c+1);
}
void aa(int a,int b)
{
int c1[2];
c1[0]=b+a;
c1[1]=a-b;
return(c1);
}

here, the address of c1 will be return. so it will store in main c cariable. we can retrive both variable via pointer,

karthi madheswaran
Leads to undefined behavior. `c1`'s lifetime ends when `aa` is left, so `c` is left pointing at a dead object. Accessing it leads to undefined behavior.
GMan
A: 

use structure and return multiple value with different data type.

Saurabh
A: 

include

include

include

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

Mystruct myfun();

int main() { char name[30]; Mystruct ms2; ms2 = myfun(); printf("val1: %d val2: %d",ms2.a,ms2.b); return 0; }

Mystruct myfun() { int a,b; Mystruct ms;

a = 10;
b = 20;
ms.a=a;
ms.b=b;

return(ms);

}

Saurabh