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.
36 messages · Page 1 of 1 (latest)
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.
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);
}
John: 2137
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.
how to put 2 into a single one? as a double?
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};
}
thank you
void f(double &x, double &y) {
x = 3.14;
y = 2.71;
}
double x;
double y;
f(x, y);
for completeness.
@candid hedge Has your question been resolved? If so, run !solved :)
@candid hedge
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.
is there a version without :: or -> ? a simpler one? because I haven't learnt about these yet
;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);
}
John: 2137
Ok! I'll try!
But why I can't just write int arr[] = {a,b}; return arr; and then use it like this in another function: int arr = function(); a = arr[0], b = arr[1]? Tuples won't work, it seems like I have to install something for it to function!
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
assignment! but both actually
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) );
or use std::pair like ^ Toeger shown before
the function returns a tuple, so should it be written tuple function()?
yes, just like in the example I shown you before
;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);
}
Multiple Returns
but I don't want to return elements from function parameters
I want to officially announce that I did it! After 12 hours of trying! Thank you!
congratz 💪