递归的功能级别范围的更清晰的解释 [英] clearer explanation of function level scope for recursion

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

问题描述

这是神经科学家Matlab"一书中的一个例子.我不了解每次递归后为g分配新值的顺序或原因.我也不明白为什么在代码的最后一行中包含"factorial2".

This is an example from the book 'Matlab for Neuroscientists'. I don't understand the order in which, or why, g gets assigned a new value after each recursion. Nor do I understand why "factorial2" is included in the final line of code.

这是文本链接

基本上,我是在要求有人重新写出作者对该功能的工作原理的解释(红色圆圈),就像他们在向5岁的孩子解释概念和过程一样.我是编程的新手.我以为我通过阅读另一本书就理解了这是如何工作的,但是现在作者的解释只引起混乱.非常感谢任何可以提供帮助的人!

Basically, I am asking for someone to re-word the authors explanation (circled in red) of how the function works, as if they were explaining the concept and processes to a 5-year old. I'm brand new to programming. I thought I understood how this worked from reading another book, but now this authors explanation is causing nothing but confusion. Many thanks to anyone who can help!!

推荐答案

递归方法的工作原理是每次调用该方法时,将一个较大的问题分解成较小的问题.是一个困难的问题;阶乘求和,变成一系列较小的问题.

A recursive method works by breaking a larger problem into smaller problems each time the method is called. This allows you to break what would be a difficult problem; a factorial summation, into a series of smaller problems.

每个递归函数都有2个部分:
1)基本情况:我们关心评估的最小值.通常,该值为零或一.

Each recursive function has 2 parts:
1) The base case: The lowest value that we care about evaluating. Usually this goes to zero or one.

if (num == 1)
  out = 1;
end


2)一般情况:一般情况是我们要调用的情况,直到达到基本情况为止.我们再次调用该函数,但是这次比开始的前一个函数少1.这使我们能够朝基本情况努力.


2) The general case: The general case is what we are going to call until we reach the base case. We call the function again, but this time with 1 less than the previous function started with. This allows us to work our way towards the base case.

out = num + factorial(num-1);

此语句意味着我们要首先调用比函数少1的函数;我们从三个开始,下一个调用从两个开始,之后的调用从1开始(这会触发我们的基本情况!)

This statement means that we are going to firstly call the function with 1 less than what this function with; we started with three, the next call starts with two, the call after that starts with 1 (Which triggers our base case!)

达到基本情况后,方法递归" .这意味着它们向后反弹,回到调用它的函数中,并从其下面的函数中带走所有数据!
在这一点上,我们的求和实际上发生了.

Once our base case is reached, the methods "recurse-out". This means they bounce backwards, back into the function that called it, bringing all the data from the functions below it!
It is at this point that our summation actually occurs.

一旦达到原始功能,我们便完成了最后的求和.

Once the original function is reached, we have our final summation.

例如,假设您要对前三个整数求和. 第一个递归调用传递给数字3.

For example, let's say you want the summation of the first 3 integers. The first recursive call is passed the number 3.

  function [out] = factorial(num)
     %//Base case
     if (num == 1)
        out = 1;
     end
  %//General case
  out = num + factorial(num-1);

遍历函数调用:

Walking through the function calls:

factorial(3); //Initial function call

//Becomes..
factorial(1) + factorial(2) + factorial(3) = returned value

这给我们6的结果!

这篇关于递归的功能级别范围的更清晰的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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