递归 C void 函数和 return 关键字 [英] Recursive C void function and return keyword

查看:36
本文介绍了递归 C void 函数和 return 关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道:

void RecFoo1(int bar){
  if (bar == 0)
    return ;
  RecFoo1(bar - 1);
}

void RecFoo2(int bar){
  if (bar == 0)
    return ;
  return RecFoo2(bar - 1);
}

我确信放置 return 关键字总是更好.如果递归函数不是 void 函数,则会收到来自 -Wreturn-type 的警告.但是这两段代码是否以相同的方式编译/执行?机器的内部差异是什么?

I am convinced that it is always better to put the return keyword. If the recursive function is not a void function, one will get a warning from -Wreturn-type. But are these two piece of code compiled/executed the same way or not ? What are the internal differences for the machine ?

我的函数示例很愚蠢,但它构成了一种最小的示例...

My example of function is stupid but it constitute a kind of minimal example...

推荐答案

C 标准对此非常清楚,您的 RecFoo2 示例不是有效的 C 程序:

The C standard is quite clear on this, your RecFoo2 example is not a valid C program:

6.3.2.2 无效

void 表达式的(不存在的)值(具有类型 void) 不得以任何方式使用,...

The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, ...

6.8.6.4 返回语句

6.8.6.4 The return statement

约束

带有表达式的 return 语句不得出现在函数中其返回类型为 void.

A return statement with an expression shall not appear in a function whose return type is void.

这篇关于递归 C void 函数和 return 关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆