473,289 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,289 developers and data experts.

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

SwissProgrammer
220 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 7813
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
220 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
220 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
626 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
220 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,426 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
220 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,426 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

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

Similar topics

6
by: Lupe | last post by:
hi, if someone can help me I would be grateful when I do def function kjklj llklç return variableA, variableB
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
1
by: turtle | last post by:
I need to write an update query that will return the earliest date to a table based on the data of a different table. I have a labor table that looks like this TableLabor JobCode WorkORder ...
5
by: Tim Gallivan | last post by:
I have 2 functions, CheckThis and CheckThat. Each function can return some of the error codes in the Enum below (CheckThis can return 0, 1, 2 or 3 and CheckThat can return 0, 4, 5 or 6). I'm trying...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
17
by: binjobster | last post by:
Hello everyone, I'm updating some documents that describes so many C/C++ functions/method(about 2500). But I'm not familiar with these codes. So I want to find such a utility can generate...
1
by: v4u2chat | last post by:
Do I need to extend any of classes from AXIS to return multiple values? I'm exposing the following method as web service through AXIS to return multiple values. public ContactAddress...
4
by: ashokbio | last post by:
I want to return values of two arguments through a function via same argument. Example: (a, b) = getvalue(x, y) Can anyone help?
1
by: ashokbio | last post by:
How to return multiple values by passing multiple arguments in function or subroutine using VB6? T. Ashok Kumar
2
ADezii
by: ADezii | last post by:
The incentive for this Tip was an Article by the amazing Allen Browne - I considered it noteworthy enough to post as The Tip of the Week in this Access Forum. Original Article by Allen Browne ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.