递归小函数

Posted 焓青

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归小函数相关的知识,希望对你有一定的参考价值。

利用递归求1+2+3+4+……+N

#include<iostream>

using namespace std;

int sum(int n)
{
if(n == 0)
{
return 0;
}
return sum(n - 1)+n;//注意返回式子的书写
}

 

int main(void)//主函数为空不需要返回值
{
int n=0;
cin >> n;
cout<<sum(n)<<endl;

}

以上是关于递归小函数的主要内容,如果未能解决你的问题,请参考以下文章