难以理解某个函数的元素是如何工作的
Trouble understanding how elements of a certain function work
我需要完全理解以下代码:
#include <iostream>
using namespace std;
double area(double length, double width);
double time(double p_area, double h_area, double mow_rate);
int main() {
double d_plot_length, d_plot_width, d_home_side, d_mow_rate;
double plot_area, home_area, time_taken;
// I've used double for all of these to get the most precise values possible, something I'd only really consider doing on small programmes such as this
cout << "What is the length of the plot? In meters please." << endl;
cin >> d_plot_length;
cout << "What is the width of the plot? In meters please." << endl;
cin >> d_plot_width;
cout<< "What is the size of the side of the house? In meters please." << endl;
cin >> d_home_side;
cout << "What is the rate at which you are going to be mowing? In meters per minute please" << endl;
cin >> d_mow_rate;
// Just getting all the data I need from the user
plot_area = area(d_plot_length, d_plot_width);
home_area = area(d_home_side, d_home_side);
time_taken = time(plot_area, home_area, d_mow_rate);
cout << "It will take " << time_taken << " minutes to mow this lawn. Better get cracking" << endl;
return 0;
}
double area(double length, double width) {
double value;
value = length * width;
return value;
}
double time(double p_area, double h_area, double mow_rate) {
double value;
value = (p_area - h_area) / mow_rate;
return value;
}
我很难理解time()
函数是如何工作的。
到目前为止,我了解到:
time_taken
从time()
函数中得到其值:time(plot_area, home_area, d_mow_rate)
。
time()
函数从底部的函数声明中获取其值。
double time(double p_area, double h_area, double mow_rate) {
double value;
value = (p_area - h_area) / mow_rate;
return value;
}
然而,这正是我陷入困境的地方。用户被要求输入d_plot_length
、d_plot_width
等的值。因此,我无法理解编译器如何知道这些值p_area
和h_area
的实际值。
我意识到area()
函数在某种程度上被用来帮助time()
函数,但据我所知,time()
函数中的变量P_area
等没有分配值。
请有人填补我理解上的空白。
更确切地说,我想知道time_taken
从流程开始到cout
在屏幕上是如何显示的。就像我说的,我熟悉大多数领域,但不是所有领域。
在您的程序中,您计算了以下值:
plot_area=面积(d_plot_length,d_plot_width);home_area=区域(d_home_side,d_home_sde);
当方法area(double,double)
被调用时,结果double
值被存储在这些变量中。
然后进行函数调用:time_taken = time(plot_area, home_area, d_mow_rate);
这是按值调用类型的函数调用。变量plot_area
、home_area
和d_mow_rate
中的值的副本被传递给函数。在time(double, double, double)
中,计算是基于您在此方法中定义的逻辑进行的,结果值将返回给main()
方法中的函数调用。
请注意,函数调用为call by value
,因此,即使main()
和函数调用中的变量名称相同,也只会将值的副本传递给函数time(double, double, double)
中提到的参数。
为了进一步阅读,我建议你看看以下链接:
- 呼叫方价值
- 呼叫方参考
- 呼叫方指针
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 链表c++插入,所有情况都已检查,但没有任何工作
- 当 int 方法工作正常时,void 方法有何不同,或者为什么我不能调用 void 方法?