具有多个功能的递归调用 [英] Recursion call with more than one function

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

问题描述

大家好!

我有一个关于递归的问题.

我有2个函数分别称为f1和f2.在f1的主体中,我们称为f2函数,在f2的主体中,我们称为f1函数,就像这样

Hello everybody!

I have a question about recursion.

I have 2 functions called f1 and f2. In f1''s body we call f2 function, in f2''s body we call f1 function, like this

return_type1 f1(parameter list 1)
{
   f2(parameter list 2);
   ...
}

return_type2 f2(parameter list 2)
{
   f1(parameter list 1);
   ...
}



但这没有用.我认为必须有一些东西(例如关键字),以便编译器知道这种递归类型.



But it didn''t work. I think there must be some stuff (like keyword) so that the compiler would know a bout this type of recursion.

Any help would be appreciated!

推荐答案

没有这样的关键字,仅仅是因为递归不是问题.

过去,在某些计算机语言中,不允许递归.后来,递归进入了其中的某些编程语言,如果我没记错的话,某些语言的一些作者真的引入了一个特殊的关键字来指示允许递归的地方.

但是,很快,一些聪明的程序员设法说服那些语言的作者,说他们是完全的白痴,因为如果聪明的程序员使用递归就不会有问题.这些作者假装他们也很聪明,但实际上感到羞耻并停止发明这种愚蠢的计算机语言.

从那时起,递归变得司空见惯,被广泛使用,并且可能没有人会使用不允许或限制递归的那些过时的编程语言.

与递归相关的错误是可能的,但它们是最容易检测到的错误,因为会引发堆栈溢出异常.查找问题的根源是编程历史上已知的最简单的调试任务之一.

现在,请参阅我对问题的评论. 不起作用"不提供信息;并且您演示的伪代码示例是相当合法的.这称为相互递归,请参阅 http://en.wikipedia.org/wiki/Mutual_recursion [^ ].

您可以以某种方式破坏实施.一种可能的原因是无限"递归.您需要为递归的结束定义一个条件,否则将导致堆栈溢出.糟糕的是,您没有共享问题的描述,也没有创建任何代码示例.因此,您将不得不自己调试代码.

这很容易-请参见上面.只需使用调试器即可.

祝你好运,
—SA
There is no such keyword, simply because recursion is not a problem.

In the past, in some computer languages recursion was not allowed. Later on, recursion made its way into some of those programming languages, and some authors of some languages, if I''m not much mistaken, really introduced a special keyword to indicate where recursion is allowed.

However, pretty soon some clever programmers manages to convince the authors of those languages that they have been complete idiots because recursion is not a problem if used by a clever programmer. Those authors pretended that they were clever, too, but in fact felt ashamed and stopped to invent such stupid computer languages.

Since that time, recursion become a commonplace, is widely used, and probably no one is going to use those obsolete programming languages where recursion is not allowed or limited.

The bugs related to recursion are possible, but they are one of the most easiest to detect, because stack overflow exception is thrown. Finding of the source of the problem is one of the most easy debugging tasks known in the history of programming.

Now, please see my comment to the question. "Not working" is not informative; and the pseudo-code sample you demonstrated is quite legitimate. This is called mutual recursive, please see http://en.wikipedia.org/wiki/Mutual_recursion[^].

You could screw up implementation somehow. One possible reason is "infinite" recursion. You need to define a condition for the end of recursion, otherwise you will get the stack overflow. To bad you did not share description of your problem and did not create any code sample. So, you will have to debug the code by yourself.

This is easy — please see above. Just use the Debugger.

Good luck,
—SA


除了解决方案1和解决方案2中的所有好的建议之外,如果不起作用"则表示不起作用"编译",这是因为在所示示例中,您需要声明对"f2"的正向引用.直线C需要它来解析函数调用.

Besides all the good advice in Solution 1 and Solution 2, if by "it doesn''t work" you mean "it doesn''t compile", that would be because in the example shown, you need to declare a forward reference to "f2". Straight C needs that in order to resolve the function calls.

return_type2 f2(parameter list 2);

return_type1 f1(parameter list 1)
{
   f2(parameter list 2);
   ...
}
 
return_type2 f2(parameter list 2)
{
   f1(parameter list 1);
   ...
}


没有关键字.
尝试调试(逐步)代码,以查看是否可以跟踪从函数f1到函数f2以及从f2到f1的流.
There is no keyword.
Try debugging (stepping through) your code to see if you can trace flow from function f1 into function f2 and from f2 into f1.


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

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