--- id: c-functions-recursion title: "C Recursion" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["recursive function", "base case", "factorial recursion", "C μž¬κ·€"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["c", "programming-language", "w3schools", "functions", "recursion"] raw_sources: ["https://www.w3schools.com/c/c_functions_recursion.php"] applied_in: [] github_commit: "" --- # [[C Recursion]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Every recursive function in the source's examples relies on a single structural guarantee: the function ONLY calls itself when a shrinking condition still holds (`k > 0`, `n > 0`, `n > 1`) and returns a plain value OTHERWISE β€” the source's own warning that recursion "can be quite easy to slip into writing a function which never terminates" is really pointing at what happens if that shrinking condition is missing or wrong, not at recursion itself being inherently risky. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Recursion** β€” a function calling ITSELF, breaking a complex problem into smaller instances of the same problem. [S1] - **Base case** β€” the condition under which the function STOPS calling itself and returns directly (e.g. `k > 0` becoming false), essential for termination. [S1] - **Unwinding accumulation** β€” each recursive call's `return` combines its own contribution with the result of the smaller sub-call (`return k + sum(k - 1);`), building up the final answer as calls return in reverse order. [S1] - **Risk of non-termination or excess resource use** β€” without a solid base case, recursion can run forever or exhaust memory/processor time. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Summing 1 to 10 via recursion: `int sum(int k) { if (k > 0) { return k + sum(k - 1); } else { return 0; } }` β€” unfolds as `10 + (9 + (8 + ... + sum(0)))`. [S1] - Countdown via recursion: `void countdown(int n) { if (n > 0) { printf("%d ", n); countdown(n - 1); } }` β€” stops calling itself once `n` reaches 0. [S1] - Factorial via recursion: `int factorial(int n) { if (n > 1) { return n * factorial(n - 1); } else { return 1; } }` β€” factorial(5) = 5Γ—4Γ—3Γ—2Γ—1 = 120; by definition 0! is also 1. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **μž¬κ·€μ˜ μ’…λ£Œ 쑰건이 핡심 μœ„ν—˜ μš”μ†Œ**: μ’…λ£Œλ˜μ§€ μ•ŠλŠ” ν•¨μˆ˜λ‚˜ κ³Όλ„ν•œ λ©”λͺ¨λ¦¬/CPUλ₯Ό μ“°λŠ” ν•¨μˆ˜λ₯Ό μ‹€μˆ˜λ‘œ μž‘μ„±ν•˜κΈ° μ‰½λ‹€λŠ” κ²½κ³ κ°€ μžˆμ§€λ§Œ, μ΄λŠ” κ²°κ΅­ μΆ•μ†Œ 쑰건(base case)이 μ—†κ±°λ‚˜ 잘λͺ»λμ„ λ•Œ λ°œμƒν•˜λŠ” λ¬Έμ œμž„μ΄ μ˜ˆμ œλ“€μ—μ„œ κ³΅ν†΅μ μœΌλ‘œ λ“œλŸ¬λ‚¨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) 5의 νŒ©ν† λ¦¬μ–Ό(5Γ—4Γ—3Γ—2Γ—1=120)을 μž¬κ·€λ‘œ κ³„μ‚°ν•˜λŠ” μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) A recursive function with a clear base case that guarantees termination (C): ```c int factorial(int n) { if (n > 1) { return n * factorial(n - 1); } else { return 1; // base case } } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[C Functions Parameters]], [[C Memory Allocate]], [[C Functions Pointers]] - **μ°Έμ‘° λ§₯락:** μž¬κ·€ β€” ν•¨μˆ˜ 포인터(Functions Pointers) μ±•ν„°λ‘œ 이어짐, μŠ€νƒ λ©”λͺ¨λ¦¬(Memory Allocate μ±•ν„°μ˜ μŠ€νƒ μ˜€λ²„ν”Œλ‘œμš° κ°œλ…)와 μ—°κ²°. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C Recursion β€” https://www.w3schools.com/c/c_functions_recursion.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Recursion" page (Astra wiki-curation, P-Reinforce v3.1 format).