Matlab的返回控制如何在递归函数中运行?

时间:2018-02-16 02:14:12

标签: matlab recursion return

它会返回递归函数还是调用递归函数的函数?

1 个答案:

答案 0 :(得分:0)

在我看来,它将首先返回到递归函数。

一点点测试:

功能accu.m

function [AN] = accu(num,an)
  disp(['recursive function with num = ' num2str(num)])
  if (num==0) 
      AN=an;
      return
  end
  AN=accu(num-1,an+num);
  disp(['recursive function with num = ' num2str(num) '.'])
end

当我们致电accu(5,0)时,它会返回:

recursive function with num = 5
recursive function with num = 4
recursive function with num = 3
recursive function with num = 2
recursive function with num = 1
recursive function with num = 0
recursive function with num = 1.
recursive function with num = 2.
recursive function with num = 3.
recursive function with num = 4.
recursive function with num = 5.

ans =

    15