C++ 问题~

来源:百度知道 编辑:UC知道 时间:2024/05/30 07:52:54
void Power (int x, int y)
{
int result;
result = 1;
while (y >0)
{
result = result * x;
y--;
}
}
哪错?

求x的y次方,结果放在局部变量result里,Power函数没有返回值,这样的话result似乎没有什么用,既没有输出,也没有返回。

语法没错,逻辑没看出什么问题,如果有什么更切实一点的问题,百度Hi我

int Power (int x, int y)
{
int result;
result = 1;
while (y >0)
{
result = result * x;
y--;
}
return result;
}

你这个字函数算的是x的y次方,
逻辑什么的没什么问题,
问题是,你怎么将计算结果给调用端呢?

做一个返回值吧:
int Power (int x, int y)
{
int result;
result = 1;
while (y >0)
{
result = result * x;
y--;
}
return result;
}

没有返回值,这个函数没有实现原有的功能