Returning multiple values from function

Hi all,

1. How to return multiple values from the function(for example how to return the four multiple float values from the function)? I tried in internet, but not satisfied with the answer. Could anyone send the document/link for the same.

2. What is the format for the doc string in cpp?

Suggestions are appreciated.


Thank you,
Shruthi LS

1. You can use multiple arguments passed by reference. Or you could package them in a single object like a tuple or a struct. I believe you can now use "structured bindings" (which seem to do roughly the same thing as returning a tuple). If they are all of the same type you could just put your multiple values in an array or vector.

2. c++ doesn't have doc strings like python's. Though a few comments at the start of your functions never did any harm ...

There do seem to be a few dangers inherent in the practice of learning python first and then assuming that its grammar will carry over to other languages.
Last edited on
Thank you so much for your response. @lastchance

It always helped me. Thank you so much for sharing your knowledge. @lastchance

Indeed! Learning python first, and assume everything is same in other languages too, which is very bad way of thinking.
A structured binding example expanded from https://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address/comment-page-3/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <tuple>
#include <iostream>
 
std::tuple<int, double, std::string> returnTuple()
{
    return { 5, 6.7 , "Hello Sailor"};
}
 
int main()
{
    auto [a, b, c]{ returnTuple() };
    std::cout << a << ' ' << b << ' ' << c << '\n';
 
    return 0;
}


5 6.7 Hello Sailor
Program ended with exit code: 0
Thank you @againtry
Topic archived. No new replies allowed.