Function with more than one return value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tapasi ghosh
    New Member
    • Nov 2007
    • 2

    Function with more than one return value

    Dear All
    I want to make a function in C++ language where there will more than one return value by reference simultaneously.
    How can I do this.
    Help needed. Thanks in advance.
    regards.
    Tapasi
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    If the objects are of the same type, return them in an array.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by sicarie
      If the objects are of the same type, return them in an array.
      Functions cannot return an array.

      You cannot return more than one type.

      Actuallty, the only time you ever want top return a value is when you want your function to be usable as an RVAL. That is, it will be used on the right-side of an assignment operator:
      [code=c]
      int result = MyFunction();
      [/code]

      Here MyFunction() needs to return an int.

      In all other cases you do not use the return type.

      For several types to be returned simulataneously , use output arguments. If the function has to create the object, then pass in the address of a pointer so the function can out the address of the newly created onject inside it. If the object already exists and the functions need to update it, then pass the object by reference.

      This allows the return type to be a completion code that that caller can test to see what happened.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by weaknessforcats
        Functions cannot return an array.

        You cannot return more than one type.
        Dang, getting C++ and Java confused again. Sorry about that.

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          Originally posted by tapasi ghosh
          I want to make a function in C++ language where there will more than one return value by reference simultaneously.

          The only way to return more than one value is to group the values together in a class or structure.

          You can return a class or structure by reference, but you have to guarantee that the object will still exist after the function is called.

          Usually, a return by reference means the object or function that returns the reference controls the deletion of the object. If you want to pass resource control of the object to the calling function, you usually pass a pointer.

          Comment

          Working...