在返回类型为void的函数中,语句return的含义是什么;

时间:2014-11-05 18:02:59

标签: c

在具有void返回类型的函数中,返回什么;声明给出? 例如,对于链表: -

void fun1(struct node* head)
{
  if (head == NULL)
    return;
  fun1(head->next);
  printf("%d  ", head->data);
}

链表的第一个节点是head。预期输出是所有节点的逆序。但是如何?

2 个答案:

答案 0 :(得分:0)

return函数的void意味着该函数不会继续运行并且不会立即向其调用者返回任何内容,我们使用它来减少函数运行的时间,当我们别无他法。

在你的情况下,它是递归函数(一个自己调用的函数)。

http://en.wikipedia.org/wiki/Recursion

你有一个if(作为停止条件)当你没有head时会被激活 所以没有什么可做的,你就是"返回"跳过下一行只是退出该功能。

答案 1 :(得分:0)

  

如何?

if(head == NULL) return;    // Leave if nothing left to do
fun1(head->next);           // Process the next (later) element now with recursion
printf("%d  ", head->data); // Print the current element

return;只是结束它执行的函数,仅在返回类型为void的函数中有效。

相关问题