C++ Return 2 values from function or return multiple values from function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SwissProgrammer
    New Member
    • Jun 2020
    • 220

    C++ Return 2 values from function or return multiple values from function

    I put this here for me as part of a repository for some of my worked through code.

    Do not return a pointer to an array.

    Put your multiple values into a class and return that class;
    or
    concatenate your multiple values into a string and return that string.

    If Bjarne Stroustrup (or ISO) publishes a version of C++ that includes functions that can return multiple values, then that could be an option.

    If you believe in Jesus, then have a nice day.
    Last edited by SwissProgrammer; Mar 11 '21, 06:40 AM. Reason: Fixed the post.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Functions in C++ need to return two things: data and status. The return value is for status, like did the function error out. Data is reurned through an output argument which in C++ is a struct/class reference.

    Comment

    • SwissProgrammer
      New Member
      • Jun 2020
      • 220

      #3
      Returning ONE value that holds multiple values:
      1. Create a class that holds multiple items and use and return that.
      2. Concatenate all of the values into a single string and return that. Then parse that string for your multiple values.


      If Bjarne Stroustrup (or ISO) publishes a version of C++ that includes functions that can return multiple values, then that could be an option.

      Thank you weaknessforcats .
      Last edited by SwissProgrammer; Mar 11 '21, 06:42 AM. Reason: Fixed response.

      Comment

      • SwissProgrammer
        New Member
        • Jun 2020
        • 220

        #4
        I did not want to explain this because if someone that does not use use pointers (and references) correctly in their function return then they could make a big mess.

        I received a private link to someone that did explain this and put it into a video. I think that the video is well done. I like it. It explains how this can be done. It does not explain why it could be disastrous to a program if it is done wrong, but it does explain a way to do it right.

        Here [X] is the link. BE CAREFUL doing this !

        If you do not know why this could be a big problem if done wrong, then do NOT do this.

        Be careful. If you do not know why this this could be a problem for your program then stay away from it.

        Comment

        • dev7060
          Recognized Expert Contributor
          • Mar 2017
          • 655

          #5
          From the video description,
          We can return more than one value from a function. One way to do this is with the help of pointers. This method is called "call by reference".
          The way shown in the video is not call by reference. Plus I don't think calling it as "Return multiple values from a function" would be appropriate.

          Comment

          • SwissProgrammer
            New Member
            • Jun 2020
            • 220

            #6
            dev7060, Thank you.

            I did put in a warning twice. Thank you for adding that the video does not do as it says. It does change two values, but not in the manner that it says.

            I know that but I did not want to get into a deep discussion about what is involved and how the video was not described correctly. It says that it returns two values, but it does not. It returns a void. It just changes two values that were created already in the calling function. A dangerous way to do this if the programmer does not do it correctly.

            What I wanted to say and did say at the start was the following:
            Do not return a pointer to an array. <<< The most serious part!

            Put your multiple values into a class [or struct or etc.] and return that class;
            or
            concatenate your multiple values into a string and return that string. <<< The best and safest way.

            Then I was informed of this video, which gave some indication of what is involved. But, still it left a LOT out. And, I put in a warning to stay away from this process if the user does not know the potential problems.

            Thank you dev7060. Your wisdom is again appreciated.

            I did not intend for this article to go this far. But, still thank you.

            My intent was to help others without swamping them. I feel sorry for people that are still using Microsoft Visual Studio (and .NET and ActiveX) and I am trying to help them to escape by giving them code and etc. In the past I used Visual Studio a huge amount until I retired. I programmed industrial programs in it that worked well.

            Then with lots of time available, I decided to learn C++. It is like a whole new world of possibilities and now I regret not learning C++ 20 years ago. I understand other people's loyalty to VS, but I am trying to gently help them to see that they can actually escape Microsoft Visual Studio (ALL of it).

            As I said, I did not intend for this article to go this far. But, still thank you.

            Thank you dev7060.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Are you asking the question or are you offering advice?

              Some of the standard library functions return multiple values (qsort, div, ldiv, etc). They illustrate just a few of the many possible strategies for doing this. I think it is more useful to assess each strategy based on the limitations of your program. For example, maybe thread-safety is important to you, maybe it isn't.

              The only technique that comes to mind as being always and invariably wrong is using the returned pointer to a variable after that variable has gone out-of-scope.

              Comment

              • SwissProgrammer
                New Member
                • Jun 2020
                • 220

                #8
                donbock, thank you.

                "Are you asking the question or are you offering advice?"

                I put this here for me as part of a repository for some of my worked through code.

                Of all the sites that I have been using, this is the site that has had the most of the directly useful "applied" code discussions for me. What does some syntax mean = I go here and elsewhere and read about the meaning. How to actually make the code do something that I find useful = I like to read what is said here. It seemed logical to me to place some of my own worked through code logic on this one site where I can find it later.

                I am writing (coding in C++11) a program, and I have have been testing various examples of applied code that I have found on the internet, and I have found almost all of those from other sites (strictly viewed via C and up to C++11) to not be useful. Not just un-useful, but aggressively anti-useful. I have not seen that propensity here on this site. A lot of Visual Basic and Visual C++ and .net and ActiveX examples show up here, but I have learned (to some extent) how to filter those out. Thus, this is where I have been occasionally placing some of my worked through code and worked through logic. Plus, I get the added benefit of people like you fixing what I thought was correct to become correct. Thank you all so much.

                Your statement of "The only technique that comes to mind as being always and invariably wrong is using the returned pointer to a variable after that variable has gone out-of-scope." is a compelling logic to not return a pointer to an array of values. Within my limited understanding, and I do still consider myself a beginner at C++, I agree with you.

                But, Stroustrup has published some amazing updates to C++, and if he tells the ISO how to do this safely, then that is a future possibility of which I did not want to forget to be aware. "If Bjarne Stroustrup (or ISO) publishes a version of C++ that includes functions that can return multiple values, then that could be an option."


                You mentioned qsort. I do not understand how to use this to return multiple values from a function.
                Reference [X]:
                Return value
                1) (none)
                2) zero on success, non-zero if a runtime constraints violation was detected


                You mentioned div and ldiv. I do not understand how to use these to return multiple values from a function.
                Reference[X]


                Thank you donbock.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  qsort is passed a pointer to an unsorted array. It sorts the contents of that array. I was suggesting that this could be interpreted as returning multiple values. (In the sense that it returns all the values of the sorted array.)

                  div and ldiv return a single structure that contains two members: quotient and remainder. I was suggesting that this could be interpreted as returning two values.

                  Perhaps I didn't understand what you meant by "return multiple values".

                  Comment

                  Working...