0

i want 2values to return form a funtion at a time in c++

26th Jan 2017, 5:13 AM
Hari Haran
Hari Haran - avatar
2 Answers
+ 1
Give the function the pointers to two values, then you can change the values in the function and they will also be changed in you main program. Example: void func(int *a, int *b){ *a = 5; *b = 10; } int main(){ int a, b; func(&a, &b); /* both a and b now have different values than before! */ cout << a << ", " << b << endl; return 0; }
26th Jan 2017, 5:24 AM
Robobrine
Robobrine - avatar
+ 1
(source: http://stackoverflow.com/questions/321068/returning-multiple-values-from-a-c-function ) You can also use std::pair (for 2 values) or std::tuple (for several values, in general).
26th Jan 2017, 7:03 AM
Álvaro