?
Solved

Maxium number of values returnable by single fucntion ?

Posted on 2007-03-19
8
Medium Priority
?
225 Views
Last Modified: 2010-04-01
How many values can a function return ?

I have a function which accepts several pointers. It's return type is int*

If I had the following would both be returned ?

      (*value01)++;
      return value01;
      (*value02)++;
      return value02;

Or do I even need to specify a return type because the pointers would be changed whether I returned them or not ?

Thanks.
0
Comment
Question by:andyw27
[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
  • Learn & ask questions
8 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 18751344
>> If I had the following would both be returned ?

No, you can only return one parameter. So, this code :

      (*value01)++;
      return value01;    //  <---- value01 will be returned
      (*value02)++;       // <----- this will never be executed
      return value02;    // <----- this will never be executed

There are several ways to return multiple values. Here are a few :

1) pass by pointer :

        void fun(int *value01, int *value02) {
            (*value01)++;
            (*value02)++;
            return;
        }

    call it like this :

         int val1 = 1;
         int val2 = 2;
         fun(&val1, &val2);
         // val1 now has value 2
         // val2 now has value 3

2) pass by reference :

        void fun(int &value01, int &value02) {
            value01++;
            value02++;
            return;
        }

    call it like this :

         int val1 = 1;
         int val2 = 2;
         fun(val1, val2);
         // val1 now has value 2
         // val2 now has value 3

3) return a struct :

        struct retvals {
            int val1;
            int val2;
        }

        retvals fun(retvals vals) {
            (vals.val1)++;
            (vals.val2)++;
            return vals;
        }

    call it like this :

         retvals vals = { 1, 2 };
         vals = fun(vals);
         // vals.val1 now has value 2
         // vals.val2 now has value 3
0
 

Author Comment

by:andyw27
ID: 18751403

Thanks thats a great answer.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18751715
As it is C++ I would suggest to create a class like
class CCounters
{
public:
   CCounters(): m_Value1(0), m_Value2(0) {}
    void IncValue1() {m_Value1++;}
    void IncValue2() {m_Value2++};
    void IncAll() {IncValue1(); IncValue2();}
private:
   int  m_Value1;
   int M_Value2;
}
...
CCounters Counters;
..
func(Counters);

...
void func(CCounter& rCounters)
{
....
  rCounters.IncAll();
}
0
Independent Software Vendors: 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!

 
LVL 4

Expert Comment

by:MacroLand
ID: 18751868
or another function could be like;

int* returnSomething(int* p1,int* p2)
{
  int* ret=new int[5];
  p1++;
  p2++;

  for (int i=0;i<5;i++)
     *(ret+i)=i;

  return ret;

}

Here both p1 and p2 is modified and you also get five different value, or as much as you can

To use it;

int main()
{
   x=5;y=7;
   int* pf=returnSomething(&x,&y);

  //now x and y are both incremented
  for (int i=0;i<5;i++)
    std::cout<<*(pf+i)<<std::endl;

return
}

Regards,
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18752203
In addition you have the memory leak.
I'm sorry, but the last suggestion I would be never recommended because it is a "dirty" solution.
I think the answer coud be:
The function can return only one value as return. This value can be a value, a pointer or a reference.

So we can return the complex object and this object can hold as many values as we need.
0
 
LVL 4

Expert Comment

by:MacroLand
ID: 18754313
>>In addition you have the memory leak.

int main()
{
   x=5;y=7;
   int* pf=returnSomething(&x,&y);

  //now x and y are both incremented
  for (int i=0;i<5;i++)
    std::cout<<*(pf+i)<<std::endl;

    delete pf; //delete the memory pointed by pf

    return 0;
}

Would you still have the memory leaks?
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18755182
In this case not any more but in real project it is very easy to forget it.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18755310
>>>  but in real project it is very easy to forget it.
Yes, normally you should delete a pointer where you created it. Only *create* functions where the only purpose is to create a new pointer are an exception from above rule. In a class you should store newly created pointers (if there is a need for pointers at all) in a data member and delete in the destructor.

Regards, Alex
0

Featured Post

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!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

777 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