将 Void 函数更改为 Return 值函数
Changing Void function to Return Value function
给定一个使用 void 函数的当前运行程序:更改程序,使 void 函数不输出变量的值,而是发送到主函数。
void trackVar(double& x, double y);
int main()
{
double one, two;
cout << fixed << showpoint << setprecision(2);
cout << "Enter two numbers: ";
cin >> one >> two;
cout << endl;
trackVar(one, two);
cout << "one = " << one << ", two = " << two << endl;
trackVar(two, one);
cout << "one = " << one << ", two = " << two << endl;
return 0;
}
void trackVar(double& x, double y)
{
double z;
z = floor(x) + ceil(y);
x = x + z;
y = y - z;
cout << "z = " << z << ", ";
}
我可以让输出正确地列出一和二的值,但不知道如何将 'z' 的输出作为主函数的一部分。
最终结果(感兴趣的人):
double trackVar(double& x, double y); ///function prototype (switched to double instead of void)
int main()
{
double one, two, z;
cout << fixed << showpoint << setprecision(2);
cout << "Enter two numbers: ";
cin >> one >> two;
cout << endl;
z = trackVar(one,two);
cout <<"z= "<<z<<" "<<"one= "<<
one<<"two= "<<two<< endl;
z = trackVar(two,one);
cout <<"z= "<<z<<" "<<"one= "<<
one<<"two= "<<two<< endl;
return 0;
}
double trackVar(double& x, double y)
{
double z;
z = floor(x) + ceil(y);
x = x + z;
y = y - z;
return z;
}
检查后,更新我的post。
试试这个:
void trackVar(double& x, double& y)
{
double z;
z = floor(x) + ceil(y);
x = x + z;
y = y - z;
}
所以,正常的函数trackVar(double x, double y)
它会将 x 和 y 的副本发送到函数中,所以即使你在那里更改它们,也不会发生任何变化。
但是,如果你使用引用,trackVar(double& x, double& y)
,在那里改变 x 和 y,它真的会改变值,所以使用引用要小心。
举个简单的例子:
void trackVar(double& a, double& b)
{
a = 99;
b = 66;
}
int main()
{
double a = 1;
double b = 2;
trackVar(a, b);
std::cout << "New a: "<< a << std::endl;
std::cout << "New b: " << b << std::endl;
return 0;
}
输出:New a: 99
输出:New b: 66
给定一个使用 void 函数的当前运行程序:更改程序,使 void 函数不输出变量的值,而是发送到主函数。
void trackVar(double& x, double y);
int main()
{
double one, two;
cout << fixed << showpoint << setprecision(2);
cout << "Enter two numbers: ";
cin >> one >> two;
cout << endl;
trackVar(one, two);
cout << "one = " << one << ", two = " << two << endl;
trackVar(two, one);
cout << "one = " << one << ", two = " << two << endl;
return 0;
}
void trackVar(double& x, double y)
{
double z;
z = floor(x) + ceil(y);
x = x + z;
y = y - z;
cout << "z = " << z << ", ";
}
我可以让输出正确地列出一和二的值,但不知道如何将 'z' 的输出作为主函数的一部分。
最终结果(感兴趣的人):
double trackVar(double& x, double y); ///function prototype (switched to double instead of void)
int main()
{
double one, two, z;
cout << fixed << showpoint << setprecision(2);
cout << "Enter two numbers: ";
cin >> one >> two;
cout << endl;
z = trackVar(one,two);
cout <<"z= "<<z<<" "<<"one= "<<
one<<"two= "<<two<< endl;
z = trackVar(two,one);
cout <<"z= "<<z<<" "<<"one= "<<
one<<"two= "<<two<< endl;
return 0;
}
double trackVar(double& x, double y)
{
double z;
z = floor(x) + ceil(y);
x = x + z;
y = y - z;
return z;
}
检查后,更新我的post。
试试这个:
void trackVar(double& x, double& y)
{
double z;
z = floor(x) + ceil(y);
x = x + z;
y = y - z;
}
所以,正常的函数trackVar(double x, double y)
它会将 x 和 y 的副本发送到函数中,所以即使你在那里更改它们,也不会发生任何变化。
但是,如果你使用引用,trackVar(double& x, double& y)
,在那里改变 x 和 y,它真的会改变值,所以使用引用要小心。
举个简单的例子:
void trackVar(double& a, double& b)
{
a = 99;
b = 66;
}
int main()
{
double a = 1;
double b = 2;
trackVar(a, b);
std::cout << "New a: "<< a << std::endl;
std::cout << "New b: " << b << std::endl;
return 0;
}
输出:New a: 99
输出:New b: 66