468,114 Members | 1,983 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
206 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.
4 Weeks Ago #1
5 3663
weaknessforcats
9,207 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.
4 Weeks Ago #2
SwissProgrammer
206 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.
4 Weeks Ago #3
SwissProgrammer
206 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.
1 Week Ago #4
dev7060
413 Expert 256MB
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.
1 Week Ago #5
SwissProgrammer
206 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.
1 Week Ago #6

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
3 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.