#Multiple Returns

36 messages · Page 1 of 1 (latest)

quick epochBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

desert meadow
#

what standard are you using?

#

you can either return a structure or pair/tuple

#

;compile cpp -std=c++17

#include <iostream>
#include <tuple>

auto name_and_age() -> std::tuple<std::string, int> {
  return { std::string("John"), 2137 };
}

auto foo(std::string name, int age) -> void {
  std::cout << name << ": " << age << '\n';
}

int main() {
  auto[name, age] = name_and_age();
  foo(name, age);
}
keen dustBOT
#
Program Output
John: 2137
untold glacier
#

You cannot return 2 variables, but you can put 2 variables into a new one and return that new single variable. Alternatively you can make an output parameter.

candid hedge
untold glacier
#
auto f() {
  return std::pair{3.14, 2.71};
}

std::pair<double, double> f() {
  return {3.14, 2.71};
}

struct Point {
  double x;
  double y;
};
Point f() {
  return {3.14, 2.71};
}
untold glacier
#
void f(double &x, double &y) {
  x = 3.14;
  y = 2.71;
}

double x;
double y;
f(x, y);

for completeness.

quick epochBOT
#

@candid hedge Has your question been resolved? If so, run !solved :)

#

@candid hedge

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

candid hedge
desert meadow
#

;compile cpp -std=c++17

#include <iostream>
#include <tuple>

using std::tuple, std::string, std::cout;

tuple<string, int> name_and_age() {
  return { string("John"), 2137 };
}

void foo(string name, int age) {
  cout << name << ": " << age << '\n';
}

int main() {
  auto[name, age] = name_and_age();
  foo(name, age);
}
keen dustBOT
#
Program Output
John: 2137
candid hedge
desert meadow
#

because of how arrays work in C++

#

in fact you shouldn't use int arr[]

#

do you need it for an assignment or do you learn by yourself

candid hedge
desert meadow
#

you may return an array but you have to use std::array for that

#

but I'd recommend sticking with std::tuple or std::pair for that

#

the reason it didn't work for you is probably because you didn't enable the C++17 language version

#

and this notation:

auto[name, age] = 

is from C++17

#

you can do this instead:

#
auto result = name_and_age();
foo( std::get<0>(result), std::get<1>(result) );
desert meadow
candid hedge
desert meadow
#

yes, just like in the example I shown you before

quick epochBOT
#

;compile cpp -std=c++17


#include <iostream>
#include <tuple>

using std::tuple, std::string, std::cout;

tuple<string, int> name_and_age() {
  return {string("John"), 2137};
}

void foo(string name, int age) {
  cout << name << ": " << age << '\n';
}

int main() {
  auto [name, age] = name_and_age();
  foo(name, age);
}

Paweł Syska
sly plank
#

Multiple Returns

candid hedge
candid hedge
desert meadow
#

congratz 💪