为了加深对函数递归调用过程中的理解,本Demo程序特意在VS2008 C#控制台程序实现了阶乘的计算功能,用于观察函数递归调用过程中的调用堆栈的情况。
源码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RecursiveTset
{
class Program
{
//阶乘的定义:n!=n*(n-1)!,特别的,1!=1;0!=1
//阶乘的实现:采用递归调用方式。主要测试和观察递归过程中的函数堆栈的调用情况!
public static int JiechengFun(int num)
{
int result=1;
if (num == 0)
result = 1;
if (num >= 1)
result = num * JiechengFun(num - 1);
return result;
}
static void Main(string[] args)
{
int res = Program.JiechengFun(4);
System.Console.WriteLine(res);
}
}
}
函数递归调用过程中的调用堆栈的情况截图如下:
