How to return 2 or more values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • group256
    New Member
    • Aug 2007
    • 2

    How to return 2 or more values?

    Hi guys
    I just want to know how I can return more than one value in a function.
    For example I send tow integers to my function and then I do some operation on them then I want to send them back again. It means in my last line of my function I have:

    return a, b;

    Is it possible? Why its not working? And if its possible how I can get them back in my main function?
    Last edited by sicarie; Aug 27 '07, 03:22 PM. Reason: Do more question marks mean it's a deeper question? I don't think so.
  • bsatheeshme
    New Member
    • Aug 2007
    • 1

    #2
    Originally posted by group256
    Hi guys
    I just want to know how I can return more than one value in a function.
    For example I send tow integers to my function and then I do some operation on them then I want to send them back again. It means in my last line of my function I have:

    return a, b;

    Is it possible????
    Why its not working????? And if its possible how I can get them back in my main function????

    Hi,

    Function with "pass by reference" is used to return more than one value.

    Comment

    • soudam
      New Member
      • Aug 2007
      • 2

      #3
      Originally posted by bsatheeshme
      Hi,

      Function with "pass by reference" is used to return more than one value.
      you can use a pointer (to what type of data you are returning) to return more than one data
      just take an array & return the base address........ .........
      thats gonna be work for sure........

      Comment

      • rohitkumar
        New Member
        • Aug 2007
        • 17

        #4
        Originally posted by soudam
        you can use a pointer (to what type of data you are returning) to return more than one data
        just take an array & return the base address........ .........
        thats gonna be work for sure........
        it will work for sure but there is one issue here. if the array is declared locally in the function and we try to return the base address, and if that base address is used to read those values it might throw some error or show some garbage value. this is because. consider the following code:
        Code:
        void main()
        {
        
        int a,b; 
        int *p;
        p=processdata(a,b);   // function taking two arguments and returning address
        cout<<"first element= "<<*p <<" second element "<<*(p+1);
            // p is pointing to first element
           // p+1 pointing to second element
        
        }
        
        int* processdata(a,b)     // function returning an address
        {
         int c[2];  // locally declaerd array
         c[0]=a;   
         c[1]=b;
         c[0]+=10;
         c[1]+=20;   // some processing on data
        return c;   // returns the base address
        }
        the code looks fine but the problem is that the address returned by processdata() function might not belong to the program. because the local variables of function are created on stack as soon as the { block is enetered and the variables are destroyed if } is encountered.
        so, when the control enters { array c is created on some address belonging to stack after } is reached the array c is destroyed alongwith its values and stack is released such that it can be used by some other functions. and if the same memory loactio used by c is overwritten by some other function cout<<*p will show some entirely diff. value. it all depends upon whether the stack is used by some other functions or not.
        i hope the concept is clear. any suggestions are welcome.

        Comment

        • gsi
          New Member
          • Jul 2007
          • 51

          #5
          HI,
          A function can never return more than one value, but u have the liberty to return an aggregate object (an array, structure). Since arrays are implicitly passed in and returned as references (pointers) in c/c++ you will land up in a dangling reference on scope exit if the array was local inside the returning function. The simplest but may not be the safest one , is to populate the return values in a structure and return it(Assuming return values does not contain references(poin ters)).

          eg,

          [code=cpp]

          struct A{
          int a,b;//depends on ur return values desired.
          };

          A foo(){
          // store the return values in a local struct A object and return
          }

          int main(){
          A returnvalues = foo();
          }


          Another way is to create the structue (to store in return values) in caller and pass it as a reference to the callee (Efficient in case of ur example).


          Thanks,
          gsi.
          [/code]

          Comment

          • cakeathon
            New Member
            • Aug 2007
            • 4

            #6
            Originally posted by bsatheeshme
            Hi,

            Function with "pass by reference" is used to return more than one value.
            Yes, this is simplest method to solve the problem, to expand on this a little:

            Rather than passing the values into the function with a copy, pass them by reference. Pass by reference can be thought of as an alias, or another name for the variables.

            Code:
            void func_name(int &a, int &b)
            {
               your function;
            }
            
            int main ()
            {
               int x;
               int y;
            
               func_name(x, y);
            
            }
            the x and y integers are passed dierectly to func_name, and they are processed/changed by the function. After the call to func_name, the main program sees their new values.

            Chris

            Comment

            • gsi
              New Member
              • Jul 2007
              • 51

              #7
              Hi,
              Rather than passing the values into the function with a copy, pass them by reference.
              Agreed, but I suppose that is better only in the case when the arguments to the function are the return values (eg, passing references to a swap function).

              Thanks,
              gsi.

              Comment

              • group256
                New Member
                • Aug 2007
                • 2

                #8
                Thanks a lot guys

                Sam

                Comment

                Working...