--- id: cpp-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.85 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "functions", "recursion"] raw_sources: ["https://www.w3schools.com/cpp/cpp_functions_recursion.asp"] applied_in: [] github_commit: "" --- # [[CPP Recursion]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The exact same three examples (range-sum via `sum(k)`, countdown, factorial) with the exact same step-by-step unfolding explanation reappear here verbatim from C's Recursion chapter β€” confirming C++'s recursion semantics, base-case requirement, and termination risks are inherited from C completely unmodified. [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 (`k > 0` becoming false), essential for termination. [S1] - **Unwinding accumulation** β€” each recursive call combines its own contribution with the result of the smaller sub-call. [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) { cout << n << " "; countdown(n - 1); } }`. [S1] - Factorial via recursion: `int factorial(int n) { if (n > 1) { return n * factorial(n - 1); } else { return 1; } }` β€” factorial(5) = 120. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - ν˜„μž¬κΉŒμ§€ 발견된 λͺ¨μˆœ 사항 μ—†μŒ (No contradictions found in available sources). C 챕터와 μ™„μ „νžˆ λ™μΌν•œ μž¬κ·€ μ˜ˆμ œμ™€ μ„€λͺ…이 확인됨. ## πŸ› οΈ 적용 사둀 (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++): ```cpp 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.85 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Function Structures]], [[CPP Functions Lambda]], [[C Functions Recursion]] - **μ°Έμ‘° λ§₯락:** μž¬κ·€ β€” λžŒλ‹€ ν•¨μˆ˜(Functions Lambda) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Recursion β€” https://www.w3schools.com/cpp/cpp_functions_recursion.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Recursion" page (Astra wiki-curation, P-Reinforce v3.1 format).