引用的函数应用

引用传递函数实现多个返回值

引用传递函数外部的变量,作为函数的返回值

int Factor(int, int&, int&);
#include <iostream>
using namespace std;

int Factor(int,int&,int&);

int main(int argc, const char * argv[]) {
    int n,s,t;
    cout<<"input:n=";
    cin>>n;
    if (!Factor(n, s, t)) {//正常返回
        cout<<"平方:"<<s<<" 立方:"<<t<<endl;
    }
    return 0;
}

int Factor(int n,int& s,int& t)
{
    if (n<0 || n>20) return 1;
    s = n*n;
    t = n*n*n;
    return 0; //正常return掉
}
input:n=3
平方:9 立方:27

results matching ""

    No results matching ""