재귀함수(Recursive Function)
= 자기 자신을 호출하는 함수 "Recursive" = 반복되는
장점 : 가독성, 구현의 용이
단점 : 성능이 떨어짐 ( 함수안에 남아있는 스택을 변수처럼 활용하기때문)
팩토리얼 구현
#include <stdio.h>
//재귀함수
//가독성, 구현의 용이
int Factorial(int count);
int main(void)
{
return 0;
}
int Factorial(int count)
{
if (count == 1)
{
return 1;
}
return count * Factorial(count - 1);
}
피보나치 수열
#include <stdio.h>
int Fibonacci(int count);
int main(void)
{
int Value = Fibonacci(10);
return 0;
}
// 피보나치수열
// 1, 1, 2, 3, 5, 8, 13, 21, 34, 55....
int Fibonacci(int count)
{
if (1 == count || 2 == count)
{
return 1;
}
return Fibonacci(count - 1) + Fibonacci(count - 2);
}
'c & c++ > c언어 기초 개념' 카테고리의 다른 글
분할구현 (0) | 2022.07.18 |
---|---|
지역변수, 전역변수 (0) | 2022.07.18 |
Visual Studio 단축키 (0) | 2022.07.15 |
변수 (0) | 2022.07.14 |
비트 연산자 (0) | 2022.07.14 |
댓글