Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Can you return 2 integers?

Posted on 2006-10-20
Medium Priority
276 Views
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
Question by:cc2006
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2
• +4

Author Comment

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

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

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

LVL 1

Expert Comment

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

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

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

0xC0DEB07 earned 92 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

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

ID: 17780267
ups sorry cc2006. careless me :(
0

LVL 2

Assisted Solution

avsrivastava earned 88 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

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

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

LVL 18

Expert Comment

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

Featured Post

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month7 days, 8 hours left to enroll