递归问题 [英] a question on recursion

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

问题描述

我们可以为main()使用递归调用函数吗?.


像::::

void add();
int main()
{
声明.....
.
.
.
.

返回add();
}

can we call a function using recursion for main().......


like::::

void add();
int main()
{
statement.....
.
.
.
.

return add();
}

推荐答案

没有显示任何递归迹象:要递归,它应该直接或间接引用自身:

That isn''t showing any sign of recursion: to be recursive, it should directly or indirectly reference itself:

int factorial(int i)
    {
    if (i > 1)
        {
        return i * factorial(i - 1);
        }
    return 1;
    }


函数main就是该函数,与其他任何函数一样.我非常确定您是说函数main是用于某些应用程序入口点的函数.充当入口点是唯一使它与众不同的事情.

我不会阻止在递归中使用它.如果您确实需要它,请使用它.这将是不寻常的,有人说这不是最好的编程风格,但我会说,这没什么不好的.当然,如果可以找到它的好用处. :-)

请在对问题的评论中看到我的问题.您的代码或伪代码没有任何递归迹象.您演示的只是转发声明,它与递归无关.因此,这使我不确定您是否知道这是什么.因此,以防万一: http://en.wikipedia.org/wiki/Recursion_(computer_science) [ ^ ].

—SA
The function main is just the function, like any other function. I''m pretty much sure you mean the function main is the one used for the entry point of some application. Serving as the entry point is the only thing which makes is special.

I does not prevent using it in recursion. Use it if you really want it. This will be unusual, and some say this is not the best programming style, but I would say, there is nothing too bad about it. If you can find a good use of it, of course. :-)

Please see my question in my comment to the question. Your code or pseudo-code does not show any signs of recursion. What you demonstrated is just forward declaration which has nothing to do with recursion. So it makes me not sure if you know what is it. So, just in case: http://en.wikipedia.org/wiki/Recursion_(computer_science)[^].

—SA


这篇关于递归问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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