Solved

Can you return 2 integers?

Posted on 2006-10-20
15
232 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now