在下面的代码中,没有表达式的return语句的作用是什么
what does the return statement without an expression do as in code below
我找不到这个返回语句工作的逻辑原因。尽管我读了很多,它应该返回undefined,但程序会直接将它带到递归调用功能低于
#include <iostream>
using namespace std;
// Recursive function to print the pattern without any extra
// variable
void printPattern(int n)
{
// Base case (When n becomes 0 or negative)
if (n ==0 || n<0)
{
cout << n << " ";
return;
}
// First print decreasing order
cout << n << " ";
printPattern(n-5);
// Then print increasing order
cout << n << " ";
}
// Driver Program
int main()
{
int n = 16;
printPattern(n);
return 0;
}
以上代码的输出是
16 11 6 1-4 1 6 11 16
递归并不是特别的。这又是你的函数,但我隐藏了名称,所以你看不到它是哪个函数。
void XXXXXXXXXX(int n)
{
// Base case (When n becomes 0 or negative)
if (n ==0 || n<0)
{
cout << n << " ";
return;
}
// First print decreasing order
cout << n << " ";
printPattern(n-5);
// Then print increasing order
cout << n << " ";
}
XXXXXXXXXX(-1(做什么?没错:它打印-1
XXXXXXXXXX(16(做什么?没错:它打印16,然后打印11的图案,然后再打印16。
现在你能明白为什么XXXXXXXXXX(16(在开头和结尾打印16吗?
return
(无论函数是否递归(将向调用者传递一个可选值,然后在调用者中继续执行。
相关文章:
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 错误:调用 .. at return 语句时没有匹配函数
- C++:我应该在 return 语句中显式使用 std::move() 来强制移动吗?
- 编译器说这个函数必须返回一个值,但我已经有一个 return 语句
- 如果 return 语句在带括号的循环中,错误可能会到达非 void 函数的末尾,但不会显示在没有括号的循环的情况下
- return 语句,该语句调用在返回 void 的函数中返回 void 的函数
- C++11 是否保证 return 语句中的局部变量将被移动而不是复制?
- return语句中的C++constexpr函数
- 用return语句结束析构函数安全吗
- return 语句将右值引用绑定到左值
- 朋友类中的no return语句
- return 语句是否阻止递归堆栈继续
- 复制 ELISION:在 return 语句中使用三元表达式时未调用移动构造函数
- cython return 语句给出了一个内置方法而不是整数
- 返回 std::string 的函数在没有 return 语句的情况下崩溃,这与返回 int 而不返回语句的函数不同
- return 语句后跟初始值设定项列表
- C++:return 语句行为怪异
- 在多个return语句的情况下,返回时带有“std::move”明智
- void函数末尾的return语句为空是否有原因
- 错误:函数中的return语句没有值,返回“void*”[-fpermission]