c++ - 为什么递归函数在 Visual Studio 中有效,即使没有返回值
问题描述
#include <iostream>
using namespace std;
int test(int numb) {
if (numb == 0) return 0;
test(numb - 1);
}
int main() {
cout<<test(10);
}
所以我正在解决一些算法问题。此代码适用于 Visual Studio,但不适用于其他在线 shell。即使没有返回,test() 函数会返回什么?还
#include <iostream>
#include<stdio.h>
using namespace std;
int test(int numb) {
if (numb == 0) return 0;
cout<<test(numb - 1)<<endl;
}
int main() {
cout<<test(10);
}
//result
0
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
号码 1349009568 是什么意思?
解决方案
您的函数在不返回任何内容时具有未定义的行为。数字 1349009568 没有意义,可能是随机的。对我来说,它显示为 0,并且与此版本具有相同的输出,永远不会返回任何内容。
int test(int numb) {
if (false) return -1;
}
推荐阅读
- apache-kafka - Apache Kafka - linger.ms 和 batch.size 设置
- dart - keytool :术语“keytool”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称
- angular - Angular 2+ 使用 ngTemplateOutlet
- android - 如何设置 React Native 应用程序的方向或语言
- prestashop - Prestashop 1.6 - 来自订单详细信息仪表板中编辑产品选项卡的附件
- filter - 为什么我的 python Butterworth 滤波器平滑信号频谱(在频率空间中)?
- angular - 将模拟 DOM 事件传递给 Angular 中的组件方法以进行单元测试
- kotlin - 如何在链内的 RXJava2 中使用 .delay() 和 .timer()?
- c# - 如何使用 ExcelReader 将数据导入数据库
- python - 从基于自定义类的视图继承的基于类的视图在父级中看不到上下文变量