在 while 循环 C++ 中使用自调用函数
Using a self calling function within a while loop C++
我是 C++ 的新手,我正在探索 while 循环内使用的自调用函数调用的行为。这是我写的代码
#include <iostream>
using namespace std;
void self_calling_function(int);
void self_calling_function(int i){
cout << "Inside function :" << i ;
while(i < 5){
i++;
cout << " I: " << i << endl;
self_calling_function(i);
}
};
int main()
{
int i = 0;
cout << "Hello world!" << endl;
self_calling_function(i);
return 0;
}
基本上我期望一旦执行进入while循环并随后调用回调自身的函数,我认为程序执行应该返回到原始的while迭代。我可以预测如果这是 Java 会发生什么,但我无法理解下面用 C++
给出的结果
编辑 1:
我期望的行为是 state/value 的 i 应该绑定到它的调用者的范围。意思是说,对于 while 的第一次迭代,i 的值从 0 变为 1,并且 1 被传递给函数。 i 的下一个序列基本上是 2。但是由于我们正在调用通过新的 while 调用再次调用 i 的函数,因此原始 while 序列的 i 的 state/value 发生了变化。
编辑 2:
这是预期的输出。
Hello world!
Inside function :0 I: 1
Inside function :1 I: 2
Inside function :2 I: 3
Inside function :3 I: 4
Inside function :4 I: 5 // after this the code shouldn't go into the while as i is not < 5)
Inside function :2 I: 3 //Here we are continuing with the original while sequence i = 2 (which got lost)
Inside function :3 I: 4
Inside function :4 I: 5 // after this the code shouldn't go into the while as i is not < 5)
Inside function :3 I: 4 // //Here we are continuing with the original while sequence i = 3
Inside function :4 I: 5 // after this the code shouldn't go into the while as i is not < 5)
Inside function :4 I: 5 // //Here we are continuing with the original while sequence i = 4 and after this the code shouldn't go into the while as i = 5 is not < 5)
查看您的评论,我认为您希望递归调用在达到 5
后结束。这可以通过将函数的参数设为引用类型来完成,如下所示:
//-----------------------------v----->i is an lvalue reference to non-const int
void self_calling_function(int &i){
cout << "Inside function :" << i ;
while(i < 5){
i++;
cout << " I: " << i << endl;
self_calling_function(i);
}
};
int main()
{
int i = 0;
cout << "Hello world!" << endl;
self_calling_function(i); //pass i by reference
return 0;
}
以上程序的输出为:
Hello world!
Inside function :0 I: 1
Inside function :1 I: 2
Inside function :2 I: 3
Inside function :3 I: 4
Inside function :4 I: 5
Inside function :5
我是 C++ 的新手,我正在探索 while 循环内使用的自调用函数调用的行为。这是我写的代码
#include <iostream>
using namespace std;
void self_calling_function(int);
void self_calling_function(int i){
cout << "Inside function :" << i ;
while(i < 5){
i++;
cout << " I: " << i << endl;
self_calling_function(i);
}
};
int main()
{
int i = 0;
cout << "Hello world!" << endl;
self_calling_function(i);
return 0;
}
基本上我期望一旦执行进入while循环并随后调用回调自身的函数,我认为程序执行应该返回到原始的while迭代。我可以预测如果这是 Java 会发生什么,但我无法理解下面用 C++
给出的结果编辑 1:
我期望的行为是 state/value 的 i 应该绑定到它的调用者的范围。意思是说,对于 while 的第一次迭代,i 的值从 0 变为 1,并且 1 被传递给函数。 i 的下一个序列基本上是 2。但是由于我们正在调用通过新的 while 调用再次调用 i 的函数,因此原始 while 序列的 i 的 state/value 发生了变化。
编辑 2: 这是预期的输出。
Hello world!
Inside function :0 I: 1
Inside function :1 I: 2
Inside function :2 I: 3
Inside function :3 I: 4
Inside function :4 I: 5 // after this the code shouldn't go into the while as i is not < 5)
Inside function :2 I: 3 //Here we are continuing with the original while sequence i = 2 (which got lost)
Inside function :3 I: 4
Inside function :4 I: 5 // after this the code shouldn't go into the while as i is not < 5)
Inside function :3 I: 4 // //Here we are continuing with the original while sequence i = 3
Inside function :4 I: 5 // after this the code shouldn't go into the while as i is not < 5)
Inside function :4 I: 5 // //Here we are continuing with the original while sequence i = 4 and after this the code shouldn't go into the while as i = 5 is not < 5)
查看您的评论,我认为您希望递归调用在达到 5
后结束。这可以通过将函数的参数设为引用类型来完成,如下所示:
//-----------------------------v----->i is an lvalue reference to non-const int
void self_calling_function(int &i){
cout << "Inside function :" << i ;
while(i < 5){
i++;
cout << " I: " << i << endl;
self_calling_function(i);
}
};
int main()
{
int i = 0;
cout << "Hello world!" << endl;
self_calling_function(i); //pass i by reference
return 0;
}
以上程序的输出为:
Hello world!
Inside function :0 I: 1
Inside function :1 I: 2
Inside function :2 I: 3
Inside function :3 I: 4
Inside function :4 I: 5
Inside function :5