简体   繁体   English

如何使用“包装函数”调用不带参数n次的递归函数?

[英]How do I use a “wrapper function” to call a recursive function without parameters n amount of times?

So for a homework assignment we have to do the partial fraction definition of the golden ratio( n +(1/(n + 1/(.....) but we need to do it recursively. OK, not a problem I can do that.... but we need to call this recursive function 1000 times and cannot use parameter functions. The professor said to limit it to a finite number of calls we can use a wrapper function, but I have never heard of this term in class and do not know where to start on the wrapper function? Could someone help me out? 因此,对于家庭作业,我们必须对黄金分割率(n +(1 /(n + 1 /(.....))进行部分定义,但是我们需要递归地进行。好吧,这不是我可以解决的问题这样做....但是我们需要调用此递归函数1000次,并且不能使用参数函数。教授说将其限制为有限数量的调用,我们可以使用包装器函数,但是我从未听说过类,不知道从哪里开始包装函数?有人可以帮帮我吗?

Please if you can tell me how to do it without sending me the solution to the partial fraction part itself, i'm doing poorly in this class and want to learn it on my own. 如果您能告诉我如何做而不给我发送部分分数本身的解决方案,请问我在这堂课上做得不好,想自己学习。 I just need help with wrapper function. 我只需要包装功能方面的帮助。 Thankyou! 谢谢!

A "wrapper function" is a function which wraps another function. “包装功能”是包装另一个功能的功能。 It's essentially another function which calls the actual function. 本质上,这是另一个调用实际函数的函数。

As a simple and unrelated example, consider you want to get the maximum value of x and 10 . 作为一个简单且无关的示例,请考虑要获得x10的最大值。 You could call std::max directly like value = std::max(x, 10) , but you don't want to pass the value 10 all the time, so you make a wrapper function: 您可以像value = std::max(x, 10)一样直接调用std::max ,但是您不想一直传递10的值,因此可以使用包装函数:

int max10(int x)
{
     return std::max(x, 10);
}

The function max10 is a wrapper function. 函数max10是包装函数。


A possible solution for your problem is to not make the actual function recursive, but the wrapper function. 对于您的问题,可能的解决方案是不使实际函数递归,而使包装器函数递归。 The wrapper function will take one argument, n , which is the number of recursions it should make. 包装函数将使用一个参数n ,这是它应该进行的递归次数。 If n is larger than zero then you call the actual function, and then make a recursive call to the wrapper function with the argument n - 1 . 如果n大于零,则调用实际函数,然后使用参数n - 1递归调用包装函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM