【问题标题】:What is the purpose of return code in this pointer function?这个指针函数中返回码的目的是什么?
【发布时间】:2017-04-15 19:37:06
【问题描述】:

在我大学的课堂上,我的老师并没有很好地解释这段代码是如何输出的。我在搜索该网站时没有遇到这种示例,我想与您分享。我还有一个问题,这个函数也像递归函数一样工作吗?

#include <stdio.h>
void F(int *a, int b)
{
    (*a)--;b+=2;
    if(*a+b<10)
    {
        printf("\n%d %d",*a,b);
        return;
    }
    (*a)--;b--;
    printf("\n%d %d",*a,b);
    F(&b,*a);
    (*a)++;b++;
    printf("\n%d %d",*a,b);
    return;
}
main()
{
    int b=5;
    F(&b,b);
    printf("\n%d",b);
    return 0;
}

【问题讨论】:

  • 是的,它调用自己,因此它是递归的。
  • 递归函数是调用自身的函数。你的函数会调用自己吗?那么,它是递归的吗?
  • @ForceBru 是的,它是一个递归函数。
  • @dozgunay,你看,现在你可以自己回答你的问题了:)
  • 如果你的老师教的是 main() 而不是 int main(void)int main(int argc, char *argv[]),并且打印前导换行符而不是尾随换行符,那么最好的办法就是取笑他/她并积极主动像这样的网站;)

标签: c


【解决方案1】:

关于你的问题:

  1. Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself. 给定的函数void F(int *a, int b) 在函数体中调用自身 ==> 所以这是一个递归。
  2. return - Terminates current function and returns specified value to the caller function. 函数 F 返回 void 这意味着什么,只是终止并返回给调用者。因为在这个具体的例子中,return 语句出现在函数结束之前,它实际上没有任何意义。如果没有 return(在这种情况下),功能保持不变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多