|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
using namespace std;
int calc(int x,int y);
int calc(int x,int y,int z);
int calc(int x,int y,int z,int n);
int main()
{
int x,y,z,n;
cout << "请输入一个整数" <<endl;
cin >> x;
calc(x,y);
cout << y <<endl;
return 0;
}
int calc(int x,int y)
{
y=x*x;
return y;
}
int calc(int x,int y,int z)
{
z=x*y;
return z;
}
int calc(int x,int y,int z,int n)
{
n=x*y*z;
return n;
}
\\为什么这个运行了之后是输入6输出的是0,试了好几个数都是输出0;
重载是没问题,只不过形式参数作为返回值不对
可以改成局部变量作返回值,然后直接传入实际参数,返回局部变量
- #include <iostream>
- using namespace std;
- int calc(int x);
- int calc(int x, int y);
- int calc(int x, int y, int z);
- int main()
- {
- int x, y, z, n;
- cout << "请输入一个整数" << endl;
- cin >> x;
- y=calc(x);
- cout << y << endl;
- return 0;
- }
- int calc(int x)
- {
- int y;
- y = x * x;
- return y;
- }
- int calc(int x, int y)
- {
- int z;
- z = x * y;
- return z;
- }
- int calc(int x, int y, int z)
- {
- int n;
- n = x * y * z;
- return n;
- }
复制代码
|
|