这是我的作业:“编写一个函数,找到两个整数输入中较大的一个(在主程序中),但允许你使用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;
}
答案 0 :(得分:3)
change
被声明为返回int
,但它永远不会返回任何内容。看起来你的函数应该返回任何内容,所以只需将其声明为void
:
void change(int& x, int& y)