470,566 Members | 1,734 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

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

SwissProgrammer
213 128KB
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.
Mar 10 '21 #1
8 7086
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 10 '21 #2
SwissProgrammer
213 128KB
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.
Mar 10 '21 #3
SwissProgrammer
213 128KB
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.
Apr 2 '21 #4
dev7060
569 Expert 512MB
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.
Apr 2 '21 #5
SwissProgrammer
213 128KB
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.
Apr 2 '21 #6
donbock
2,425 Expert 2GB
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.
Apr 23 '21 #7
SwissProgrammer
213 128KB
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.
Apr 24 '21 #8
donbock
2,425 Expert 2GB
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".
Apr 25 '21 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Lupe | last post: by
1 post views Thread by turtle | last post: by
16 posts views Thread by Nikolay Petrov | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.