void函数中使用递归时的return问题

hjf06 2010-01-09 01:37:24
今天练习二叉树的遍历程序,对于使不使用return很纠结,所以想请教一下,下面这个函数中,从思路上来讲,应该不应该加return?补充一下,在这里使用或者不适用,貌似对调用这个函数没有影响的,但这又是为什么呢?

void preorder(Tree *tree)
{
if(tree!=NULL)
{
printf("%d, ",tree->data);
preorder(tree->left);
preorder(tree->right);
}
return;
}
...全文
863 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobo364 2010-01-12
  • 打赏
  • 举报
回复
不用加,函数本来就是void了
  • 打赏
  • 举报
回复
jf
hjf06 2010-01-12
  • 打赏
  • 举报
回复
总结起来

加上,代码容易读懂;
不加上,节省资源。
macrojj 2010-01-12
  • 打赏
  • 举报
回复
[Quote=引用楼主 hjf06 的回复:]
今天练习二叉树的遍历程序,对于使不使用return很纠结,所以想请教一下,下面这个函数中,从思路上来讲,应该不应该加return?补充一下,在这里使用或者不适用,貌似对调用这个函数没有影响的,但这又是为什么呢?

void preorder(Tree *tree)
{
if(tree!=NULL)
{
printf("%d, ",tree->data);
preorder(tree->left);
preorder(tree->right);
}
return;
}
[/Quote]


return 在void 函数的最后一句 这有什么其他的作用吗。
没有啊。可以不要
mostmark 2010-01-11
  • 打赏
  • 举报
回复
加上,虽然这种情况下加和不加基本没区别
但是编程习惯问题,规范性
东大坡居士 2010-01-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 arong1234 的回复:]
这个加于不加是等效得,在没有返回值得情况下,return是可以“省略”得
[/Quote]

嗯嗯,觉得还是加上好~看得更明确
baby1986 2010-01-11
  • 打赏
  • 举报
回复
能不加的话绝对不加,return在递归中有很多妙用,对流程控制很有帮助
caoshuming_500 2010-01-09
  • 打赏
  • 举报
回复
顶一下
jernymy 2010-01-09
  • 打赏
  • 举报
回复

/*
个人觉得return的几点好处是
1. 可以使程序在遇到异常时提前返回
2. 可以使代码不用向右推移的写,例如用tab,或者4个space
*/
#include "stdio.h"

#define USE_RETUREN
#define ERROR_PRINT(str) printf("%s:%d %s !\n", __FILE__, __LINE__, str)

static int InitSystem(void)
{
printf("Init system successful !\n");
return 1;
}

static int InitApp(void)
{
printf("Init application successful !\n");
return 1;
}

static int InitWindow(void)
{
printf("Init window successful !\n");
return 1;
}

// 80 len
/*******************************************************************************
函 数 名 : TestInit
功 能 : 代码按照先后顺序初始化system, app, window
输入参数 : 无
返 回 值 : 成功返回1, 失败返回0
作 者 : jernymy
时 间 : 2010-01-09
*******************************************************************************/
#ifdef USE_RETUREN

// 此种方法可以看到代码还是比较清晰易见的了

int TestInit(void)
{
int nRet;

nRet = InitSystem();
if (!nRet)
{
ERROR_PRINT("Init system fail");
return 0;
}

nRet = InitApp();
if (!nRet)
{
ERROR_PRINT("Init application fail");
return 0;
}
nRet = InitWindow();
if (!nRet)
{
ERROR_PRINT("Init window fail");
return 0;
}
printf("Init all finished !\n");

return 1;
}

#else
// 此种方法,我们会看到不断的if...else,是code向右偏移
// 一行code比较短的时候还可以
// 但是code每行比较长的是时候,就不方便看代码了
// 建议一行的code长度不要超过80字节
int TestInit(void)
{
int nRet = 0;

nRet = InitSystem();
if (nRet)
{
nRet = InitApp();
if (nRet)
{
nRet = InitWindow();
if (nRet)
{
printf("Init all finished !\n");
}
else
{
ERROR_PRINT("Init window fail");
}
}
else
{
ERROR_PRINT("Init application fail");
}
}
else
{
ERROR_PRINT("Init system fail");
}

return nRet;
}

#endif

int main(void)
{
int nRet;

nRet = TestInit();
if (!nRet)
{
ERROR_PRINT("Init all fail");
return -1;
}

return 0;
}

hjf06 2010-01-09
  • 打赏
  • 举报
回复
是,资源要用得少,代码要写得多。。。。
liaoy747 2010-01-09
  • 打赏
  • 举报
回复
楼主有待提高.继续努力,多写点代码.
  • 打赏
  • 举报
回复
return的作用就是立刻从这个函数返回。

加不加看你的需求。

z569362161 2010-01-09
  • 打赏
  • 举报
回复
void preorder(Tree *tree) 
{
if(tree!=NULL)
{
printf("%d, ",tree->data);
preorder(tree->left);
preorder(tree->right);
}
}


浪费资源是可耻滴。
hjf06 2010-01-09
  • 打赏
  • 举报
回复
哦,谢谢。。。。
wangshiruyan1989 2010-01-09
  • 打赏
  • 举报
回复
没有返回类型的时候声明为void 此时可以不写return 语句
失落的凡凡 2010-01-09
  • 打赏
  • 举报
回复
加不加都可以 我个人觉得加上还是很好的
arong1234 2010-01-09
  • 打赏
  • 举报
回复
这个加于不加是等效得,在没有返回值得情况下,return是可以“省略”得

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧