Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

Can you return 2 integers?

Hi,

I have a function, and ideally I'd like to return two integers and might need to return two integers and a character. Is this possible, or can I only return one integer?


int main() {
...
   int i
   i = Func();
...
}

int Func()
{
   x = 2;
   y = 5;
   return(x, y);
}
0
cc2006
Asked:
cc2006
  • 3
  • 2
  • 2
  • +4
2 Solutions
 
cc2006Author Commented:
I think I might have sorted this, but not sure. Basically I'm reading a C book, and have used an example. Is a 'call by reference' OK to use in this situation? So I can use one value in the return, and then use the pointed to set the other secondary variable.
0
 
PaulCaswellCommented:
Hi cc2006,

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

MyStruct func()
{
 MyStruct rnsr;
 rnsr.a = 1;
 rnsr.b = 1;
 return rnsr;
}

Essentially you 'wrap' whatever you like in a structure. You can pass and return strucures to functions.

Paul
0
 
AlexFMCommented:
Other way is using output parameters:

int x, y;
Func(&x, &y);

int Func(int* px, int* py)
{
   *px = 2;
   *py = 5;
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
keslanCommented:
You can define numbers global before main so you don't need to return any number

int x,y;

int main() {
...
   int i
    Func();
//Here you can use x and y
i=x+y;//or what you want
...
}

int Func()
{
   x = 2;
   y = 5;
}
0
 
cc2006Author Commented:
Paul - thanks I'll have a look at using the typedef. I presume I can still call a and b in the main()

Alex - I think I might have used code like that on the call by reference.

Paul / Alex - Is there a preferred option from the two methods you mention?

Keslan - I've been told to avoid using global variables.
0
 
PaulCaswellCommented:
Hi cc206,

There isnt really a 'best' way.

If you plan to change what you pass back, as you suggested you expected to, then the structure is probably better. It is easy to change the typedef and everything still works. With Alex's method you have to change more but it can be clearer.

Paul
0
 
0xC0DEB07Commented:
I personally prefer Pauls' solution because it is more typesafe than passing pointers to variables.
Anyway there is one more option  - you could pass a pointer to a struct instead of passing the whole structure by value.
Here is how to do it:

struct myStruct
{
         int X;
         int Y;
};

myStruct str;

void Func(struct myStruct* s){
         s->X=999;
         s->Y=888;
}

int main(){
         ...
        myStruct str;
         Func(&str);
         ...
}
0
 
PaulCaswellCommented:
That is the most usual method. It avoids much memory copying because only a pointer is passed. The main difference is:

With pointers, the call sequence looks like this.

        myStruct str;
         Func(&str);
 
With structures it looks like:

        myStruct str = Func();

Personally I prefer the latter when the structure are small because it is neater. Larger structures are generally passed by reference.

Paul

 
0
 
keslanCommented:
ups sorry cc2006. careless me :(
0
 
avsrivastavaCommented:
a solution which no one has pointed out till now.
it is quite interesting; but has limited utility.
In case it is known that the numbers you are going to return as small numbers, you can actually pack the 2 or more numbers in a single integer.
here assuming that you have 32bit int.
For example, if you know that you are going to return numbers which you can fit in 1 byte(<256),
then you can put upto 4 numbers inside an int.
say, you want to  return ints x,y,z(all<256 and positive) and char w.

void Func(){
 int x, y, z;
 char w;
....
 int retval =0;
 retval |=x;
 retval |=y<<8;
 retval |=z<<16;
 retval |=w<<24;
 return retval;
}
int main()
{
 int i, j, k, r;
 char l;
 r = Func();
 i=r & 0xFF;
 j=(r>>8) & 0xFF;
 k=(r>>8) & 0xFF;
 l= (char)((r>>8) & 0xFF);
....
}

Hope you get the point.
Also you might have portability issues, so not always suggested; but it is important to know you options.
Sometime you might me stuck with a function prototype(which you are not allowed to change), then techniques like this are useful.
on the same lines packing booleans arrays as bit vectors inside integers is a valid idea most of the time.
0
 
AlexFMCommented:
For 2-3 numbers you can use parameters or structure. For 100 numbers you must use structure - umagine ugly function with 100 parameters.
0
 
avsrivastavaCommented:
typo in my post above.
 k=(r>>16) & 0xFF;
 l= (char)((r>>24) & 0xFF);
0
 
Jinesh KamdarCommented:
avsrivastava, thats a good solution under stringent conditions ... learnt smthng new today :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now