how to return multiple values from a function in c++ code example
Example 1: c++ return multiple values
#include <tuple>
std::tuple<int, int> divide(int dividend, int divisor) {
return std::make_tuple(dividend / divisor, dividend % divisor);
}
#include <iostream>
int main() {
using namespace std;
int quotient, remainder;
tie(quotient, remainder) = divide(14, 3);
cout << quotient << ',' << remainder << endl;
}
Example 2: accepting multiple values from a function in cpp
#include<iostream>
using namespace std;
void div(int a, int b, int "ient, int &remainder) {
quotient = a / b;
remainder = a % b;
}
main() {
int a = 76, b = 10;
int q, r;
div(a, b, q, r);
cout << "Quotient is: "<< q <<"\nRemainder is: "<< r <<"\n";
}
Example 3: accepting multiple values from a function in cpp
#include<iostream>
using namespace std;
void div(int a, int b, int "ient, int &remainder) {
quotient = a / b;
remainder = a % b;
}
main() {
int a = 76, b = 10;
int q, r;
div(a, b, q, r);
cout << "Quotient is: "<< q <<"\nRemainder is: "<< r <<"\n";
}