# [C언어-막코딩] 23장 재귀 함수 ----- - 참고 : [[C언어] 수학 펙토리얼(factorial) 구하기](https://steemit.com/kr-dev/@codingman/c-factorial--1550362190034) [[C언어] 피보나치 수열](https://steemit.com/kr-dev/@codingman/c--1550189431811) - 다운 사이트 : https://sourceforge.net/projects/orwelldevcpp/ - 웹컴파일러 : https://www.tutorialspoint.com/compile_c_online.php ----- 지난 시간에 사용자 정의 함수에 대해 알아 보았습니다. 오늘은 사용자 정의 함수를 만드는데 이때 자기 자신을 다시 호출하는 재귀 함수에 대해 이야기를 할까 합니다. 예전에 참고 post로 펙토리얼 구하기와 피보나치 수열을 구할 때 사용자 정의 함수로 구현해 봤었는데 이때 사용한 함수가 바로 재귀 함수입니다. 즉, 호출 된 함수가 수행하면서 다시 자신을 재 호출 하는 함수를 재귀 함수라고 합니다. 그럼 재귀 함수에 대해 간단히 살펴보도록 하죠. ## 1. 재귀 함수 ----- <center>  </center> 위 그림처럼 전달값(인자)이 3일때 aaa(3)함수가 호출 되면 전달값 3을 printf()문으로 출력 한뒤에 if문에서 조건식(a>1)을 만족하면 aaa(a-1)로 자기 자신을 재 호출하게 됩니다. 전달값이 a-1로 3-1임으로 aaa(2)함수가 호출 됩니다. 이런식으로 if문 a가 1이 될때까지 aaa()함수가 반복 호출 됩니다. 이렇게 특정 함수가 호출 되고 그 특정 함수가 처리 과정중에 자기 자신을 재호출 할 때 이걸 재귀 함수라고 부릅니다. <center>  </center> 코딩을 하면 위 그림처럼 코딩하면 5 출력 aaa(4) 호출 4 출력 aaa(3) 호출 3 출력 aaa(2) 호출 2 출력 aaa(1) 호출 1 출력 이렇게 수행 됩니다. ## 2. 코딩 ----- 말보다는 직접 코딩하고 결과를 살펴 봐야 겠죠. **[소스]** ``` #include <stdio.h> void aaa(int a){ printf("%d \n",a); if(a>1) aaa(a-1); } int main(int argc, char *argv[]) { aaa(5); return 0; } ``` **[결과]**  너무 실험 코딩이 단순하기에 뭔가 의미를 준 재귀함수를 만들어 봅시다. 1~10까지의 합을 구하는 코딩은 예전에 어떻게 했죠. ``` for(i=1;i<=10;i++){ sum+=i; } ``` 이렇게 for문으로 1~10까지 sum 변수에 더해서 저장했습니다. 이걸 재귀 함수로 만들어 봅시다. ``` int sum(int a){ if(a==1) return 1; else return a+sum(a-1); } ``` 전달값 a=10이면 if문에서 1일때는 그냥 1을 반환값으로 하고 1이 아닐때는 a+sum(a-1)을 반환값으로 하면 됩니다. 위식을 풀이하면 ``` {10+sum(9)} 10 + {9+sum(8)} 10 + 9 + {8+sum(7)} ... 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + {2+sum(1)} 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 ``` 이렇게 해서 최종 10~1까지 더한 값을 반환하게 됩니다. **[소스]** ``` #include <stdio.h> int sum(int a){ if(a==1) return 1; else return a+sum(a-1); } int main(int argc, char *argv[]) { int sumVal=0; sumVal=sum(10); printf("sum = %d \n",sumVal); return 0; } ``` **[결과]**  ## 마무리 ----- 위에 링크가 펙토리얼 구하기와 피보나치 수열 post도 전부 재귀함수를 이용한 코딩입니다. 오늘 내용을 읽어 보셨으면 참고 링크을 걸어놓은 post도 한번 다시 읽어 보시기 바랍니다. 참고로 1~10까지의 합을 재귀함수로 코딩해 봤는데 느낌이 어떤지요. 간단히 for문을 이용하면 되는데 재귀함수로 표현하니깐 좀 복잡하게 코딩이 되었네요. 그래도 재귀 함수의 원리를 이해하는데에는 합을 구하는 식이 가장 적절해 보이네요. 꼭 합을 구하는 식이 아니라도 수열에 관련해서 여러분들도 재귀함수를 구현해 보세요. 재귀함수를 알아두시면 나중에 유용하게 써먹을 수 있을 거에요. 은근 재밌는 코딩입니다. --- ##### <sub> **Sponsored ( Powered by [dclick](https://www.dclick.io) )** </sub> [](https://api.dclick.io/v1/c?x=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjIjoiY29kaW5nbWFuIiwicyI6ImMtMjMtLTE1NTU3MTg1NjE5MDAiLCJhIjpbImktMTk1Il0sInVybCI6Imh0dHBzOi8vd3d3LmRjbGljay5pby9tb25ldGl6ZSIsImlhdCI6MTU1NTcxODU2MSwiZXhwIjoxODcxMDc4NTYxfQ.Ck-Bp_3pdjPPWB8-taMU0XJweIHT1jKaRG32SDPwEmY)
author | codingman | ||||||
---|---|---|---|---|---|---|---|
permlink | c-23--1555718561900 | ||||||
category | kr-dev | ||||||
json_metadata | {"tags":["kr-dev","jjangjjangman","busy","kr","kr-join"],"app":"busy/2.5.6","format":"markdown","community":"busy","users":["codingman"],"links":["https://steemit.com/kr-dev/@codingman/c-factorial--1550362190034","https://steemit.com/kr-dev/@codingman/c--1550189431811","https://sourceforge.net/projects/orwelldevcpp/","https://www.tutorialspoint.com/compile_c_online.php","https://www.dclick.io","https://api.dclick.io/v1/c?x=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjIjoiY29kaW5nbWFuIiwicyI6ImMtMjMtLTE1NTU3MTg1NjE5MDAiLCJhIjpbImktMTk1Il0sInVybCI6Imh0dHBzOi8vd3d3LmRjbGljay5pby9tb25ldGl6ZSIsImlhdCI6MTU1NTcxODU2MSwiZXhwIjoxODcxMDc4NTYxfQ.Ck-Bp_3pdjPPWB8-taMU0XJweIHT1jKaRG32SDPwEmY"],"image":["https://cdn.steemitimages.com/DQmTk2wL2hJrUm9ZS8cnSkWU9W79PadCBFG9DQiturq2VjF/a2.jpg","https://cdn.steemitimages.com/DQmcbcbav2qFBuwjqcT8QyegEfsciRctQBZjdoN7vBQMKEA/a1.jpg","https://cdn.steemitimages.com/DQmRmz4JLn2ZuWeWfXv9wjpfKi71km7xTkn2fsMX9391m5u/a3.jpg","https://cdn.steemitimages.com/DQmZtKfApoA9FRyMJVFc6811i2f6ETb3hTu9kHRcNaXiHSv/aa1.jpg","https://s3.ap-northeast-2.amazonaws.com/dclick/image/dclick/1552477485946.png"]} | ||||||
created | 2019-04-20 00:02:42 | ||||||
last_update | 2019-04-20 00:04:24 | ||||||
depth | 0 | ||||||
children | 10 | ||||||
last_payout | 2019-04-27 00:02:42 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 1.386 HBD | ||||||
curator_payout_value | 0.453 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 3,134 | ||||||
author_reputation | 23,188,231,710,844 | ||||||
root_title | "[C언어-막코딩] 23장 재귀 함수" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 83,410,912 | ||||||
net_rshares | 3,835,894,529,369 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
guest123 | 0 | 849,479,545 | 100% | ||
busy.pay | 0 | 295,099,783,819 | 1.46% | ||
steemitboard | 0 | 14,547,157,158 | 1% | ||
skan | 0 | 623,833,143,738 | 16% | ||
sisilafamille | 0 | 557,391,971 | 100% | ||
stylegold | 0 | 2,475,225,328 | 100% | ||
apoloo1 | 0 | 4,724,391,940 | 8% | ||
trenz | 0 | 88,320,233 | 1% | ||
bramd | 0 | 1,772,227,814,936 | 20% | ||
krfeed | 0 | 276,758,334 | 100% | ||
tradingideas | 0 | 694,520,818,478 | 100% | ||
urobotics | 0 | 34,387,066,279 | 100% | ||
dj-on-steem | 0 | 6,181,459,532 | 31% | ||
codingman | 0 | 52,347,168,971 | 100% | ||
sintai | 0 | 37,656,116,322 | 100% | ||
brainstormot | 0 | 39,311,490,210 | 100% | ||
marsswim | 0 | 16,299,912,910 | 51% | ||
wuwurrll | 0 | 4,170,960,682 | 100% | ||
babymir | 0 | 5,727,075,236 | 100% | ||
raah | 0 | 73,219,354,602 | 61% | ||
dorian-lee | 0 | 22,139,909,350 | 100% | ||
anpigon | 0 | 74,180,378,665 | 100% | ||
glory7 | 0 | 13,413,381,671 | 10% | ||
votes4minnows | 0 | 433,837,753 | 1% | ||
cyberrn | 0 | 44,487,253,388 | 3% | ||
wacol413 | 0 | 2,738,878,318 | 100% |
도통 뭐가뭔지 ㅎㅎ 주말 잘보내셔요 천간님~~^^
author | marsswim |
---|---|
permlink | re-codingman-c-23--1555718561900-20190420t022628522z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2019-04-20 02:26:30 |
last_update | 2019-04-20 02:26:30 |
depth | 1 |
children | 1 |
last_payout | 2019-04-27 02:26:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 27 |
author_reputation | 35,639,311,179,218 |
root_title | "[C언어-막코딩] 23장 재귀 함수" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,414,917 |
net_rshares | 0 |
marsswim님도 주말 잘 보내세요.
author | codingman |
---|---|
permlink | re-marsswim-re-codingman-c-23--1555718561900-20190420t040304031z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2019-04-20 04:03:06 |
last_update | 2019-04-20 04:03:06 |
depth | 2 |
children | 0 |
last_payout | 2019-04-27 04:03:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 21 |
author_reputation | 23,188,231,710,844 |
root_title | "[C언어-막코딩] 23장 재귀 함수" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,418,050 |
net_rshares | 0 |
어렵습니다 ㅅㅅ
author | raah |
---|---|
permlink | re-codingman-c-23--1555718561900-20190420t003952968z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2019-04-20 00:39:54 |
last_update | 2019-04-20 00:39:54 |
depth | 1 |
children | 1 |
last_payout | 2019-04-27 00:39:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 8 |
author_reputation | 104,978,922,741,381 |
root_title | "[C언어-막코딩] 23장 재귀 함수" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,412,000 |
net_rshares | 0 |
제 설명이 부족했군요.
author | codingman |
---|---|
permlink | re-raah-re-codingman-c-23--1555718561900-20190420t040246115z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2019-04-20 04:02:48 |
last_update | 2019-04-20 04:02:48 |
depth | 2 |
children | 0 |
last_payout | 2019-04-27 04:02:48 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 12 |
author_reputation | 23,188,231,710,844 |
root_title | "[C언어-막코딩] 23장 재귀 함수" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,418,034 |
net_rshares | 0 |
Congratulations @codingman! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) : <table><tr><td>https://steemitimages.com/60x70/http://steemitboard.com/@codingman/votes.png?201904201503</td><td>You made more than 6000 upvotes. Your next target is to reach 7000 upvotes.</td></tr> </table> <sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@codingman) and compare to others on the [Steem Ranking](http://steemitboard.com/ranking/index.php?name=codingman)_</sub> <sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub> To support your work, I also upvoted your post! ###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
author | steemitboard |
---|---|
permlink | steemitboard-notify-codingman-20190420t154556000z |
category | kr-dev |
json_metadata | {"image":["https://steemitboard.com/img/notify.png"]} |
created | 2019-04-20 15:45:57 |
last_update | 2019-04-20 15:45:57 |
depth | 1 |
children | 0 |
last_payout | 2019-04-27 15:45:57 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 881 |
author_reputation | 38,975,615,169,260 |
root_title | "[C언어-막코딩] 23장 재귀 함수" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,447,136 |
net_rshares | 0 |
재귀함수 어렵죠. stack overflow도 신경써야 하고요.
author | tradingideas |
---|---|
permlink | re-codingman-c-23--1555718561900-20190420t180255298z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2019-04-20 18:02:57 |
last_update | 2019-04-20 18:02:57 |
depth | 1 |
children | 1 |
last_payout | 2019-04-27 18:02:57 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 35 |
author_reputation | 473,979,989,726,345 |
root_title | "[C언어-막코딩] 23장 재귀 함수" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,453,291 |
net_rshares | 0 |
메모장에 식을 그림으로 그리고 나서 코딩화 하면 할만 한 것 같아요. 바로 식을 코딩화 하려고 재귀 함수가 참 힘들죠.
author | codingman |
---|---|
permlink | re-tradingideas-re-codingman-c-23--1555718561900-20190421t000122923z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2019-04-21 00:01:24 |
last_update | 2019-04-21 00:01:24 |
depth | 2 |
children | 0 |
last_payout | 2019-04-28 00:01:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 66 |
author_reputation | 23,188,231,710,844 |
root_title | "[C언어-막코딩] 23장 재귀 함수" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,470,274 |
net_rshares | 0 |
재귀부터 헷갈리기 시작하더라구요. 찬찬히 다시 해봐야겠어요.
author | urobotics |
---|---|
permlink | re-codingman-c-23--1555718561900-20190423t005026620z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2019-04-23 00:50:30 |
last_update | 2019-04-23 00:50:30 |
depth | 1 |
children | 2 |
last_payout | 2019-04-30 00:50:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 33 |
author_reputation | 14,923,860,622,500 |
root_title | "[C언어-막코딩] 23장 재귀 함수" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,585,461 |
net_rshares | 0 |
처음 접할때는 쉬우면서도 약간 어려운 것이 재귀 표현이죠. 쉽게 이해하기 위해서는 먼저 a4용지를 하나 꺼내서 프로그램 처리 할 때 재호출 되는 과정을 순서대로 써내려가 보세요. 일종의 수학 풀이 과정처럼 순차적으로 호출 되면 다음 동작 호출되면 다음 동작을 a4용지에 써내려가면 감이 잡히실거에요. 머리속으로만 이해하실려고 하면 재귀 함수가 좀 혼동을 야기 시키긴 해요.
author | codingman |
---|---|
permlink | re-urobotics-re-codingman-c-23--1555718561900-20190423t015444565z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2019-04-23 01:54:54 |
last_update | 2019-04-23 01:54:54 |
depth | 2 |
children | 1 |
last_payout | 2019-04-30 01:54:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 208 |
author_reputation | 23,188,231,710,844 |
root_title | "[C언어-막코딩] 23장 재귀 함수" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,587,854 |
net_rshares | 0 |
아, 그렇군요. 자기가 자기를 부른다는 개념이 정말 큰 혼동을 줬는데요. 그렇군요. 변수도 하나씩 써보면 확실히 개념잡기 쉽겠습니다. ㅎㅎㅎㅎ 감사합니다. 쫌 발전이 있겠군요. ㅎㅎㅎㅎ
author | urobotics |
---|---|
permlink | re-codingman-re-urobotics-re-codingman-c-23--1555718561900-20190423t030541410z |
category | kr-dev |
json_metadata | {"tags":["kr-dev"],"app":"steemit/0.1"} |
created | 2019-04-23 03:05:39 |
last_update | 2019-04-23 03:05:39 |
depth | 3 |
children | 0 |
last_payout | 2019-04-30 03:05:39 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 103 |
author_reputation | 14,923,860,622,500 |
root_title | "[C언어-막코딩] 23장 재귀 함수" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 83,590,149 |
net_rshares | 0 |