Solved

Can you return 2 integers?

Posted on 2006-10-20
15
256 Views
Last Modified: 2010-04-15
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
Comment
Question by:cc2006
  • 3
  • 2
  • 2
  • +4
15 Comments
 

Author Comment

by:cc2006
ID: 17777953
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17777964
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17779032
Other way is using output parameters:

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

int Func(int* px, int* py)
{
   *px = 2;
   *py = 5;
}
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 1

Expert Comment

by:keslan
ID: 17779248
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
 

Author Comment

by:cc2006
ID: 17779562
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17779606
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
 
LVL 6

Accepted Solution

by:
0xC0DEB07 earned 23 total points
ID: 17780093
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17780105
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
 
LVL 1

Expert Comment

by:keslan
ID: 17780267
ups sorry cc2006. careless me :(
0
 
LVL 2

Assisted Solution

by:avsrivastava
avsrivastava earned 22 total points
ID: 17781197
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17782864
For 2-3 numbers you can use parameters or structure. For 100 numbers you must use structure - umagine ugly function with 100 parameters.
0
 
LVL 2

Expert Comment

by:avsrivastava
ID: 17782897
typo in my post above.
 k=(r>>16) & 0xFF;
 l= (char)((r>>24) & 0xFF);
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 17816695
avsrivastava, thats a good solution under stringent conditions ... learnt smthng new today :)
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question