错误:控件到达非void函数的末尾

Error: control reaches end of non-void function?

本文关键字:函数 void 控件 错误      更新时间:2023-10-16

这是我的任务:"编写一个函数,在主程序中找到输入的两个整数中较大的一个,但允许您使用pass-by-reference更改函数中整数的值。"!!这项作业今晚到期了!!这是我目前的代码:

#include <iostream>
using namespace std;
int change(int& x, int& y)
{
    int temp=0;
    cout << "Function(before change): " << x << " " << y << endl;
    temp = x;
    x = y;
    y = temp;
    cout << "Function(after change): " << x << " " << y << endl;
}
int main()
{
    int x,y;
    cout << " Please Enter your first number: ";
    cin >> x;
    cout << " Please Enter your second number: ";
    cin >> y;
    if (x > y)
        cout << x << " is greater than " << y << endl;
    if (x < y)
        cout << y << " is greater than " << x << endl;
    if (x == y)
        cout << x << " is equal to " <<y << endl;
    cout << "Main (before change): " << x << " " << y << endl;
    change(x, y);
    cout << "Main (after change): " << x << " " << y << endl;
    system("Pause");
    return 0;
}

change被声明为返回int,但它从不返回任何内容。看起来您的函数不应该返回任何内容,所以只需将其声明为void:

void change(int& x, int& y)