--- id: cpp-functions title: "C++ Functions" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["void function", "function call", "declaration order matters", "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"] raw_sources: ["https://www.w3schools.com/cpp/cpp_functions.asp"] applied_in: [] github_commit: "" --- # [[CPP Functions]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Calling a function BEFORE it's defined (with no prior declaration) is an explicit ERROR in C++ β€” the source shows `myFunction()` called inside `main()` at the top, with its full definition written BELOW, producing a compile error β€” a stricter behavior than what C-style top-down reading might suggest, meaning the declaration-above/definition-below organizational pattern isn't just a style preference here, it's the FIX for a real compile failure, not merely cosmetic. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Function** β€” a block of code that only runs when CALLED. [S1] - **`void` return type** β€” the function returns no value. [S1] - **Calling before defining fails without a declaration** β€” if `myFunction()` is called in `main()` but only DEFINED below `main()` with no prior declaration, it's a compile error. [S1] - **Declaration + definition split** β€” writing `void myFunction();` above `main()` and the full body below fixes the ordering problem while keeping code organized. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - The error case (calling before any declaration exists): `int main() { myFunction(); return 0; } void myFunction() { cout << "..."; } // Error`. [S1] - The fix using forward declaration: `void myFunction(); int main() { myFunction(); return 0; } void myFunction() { cout << "I just got executed!"; }`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **μ„ μ–Έ 없이 μ •μ˜λ³΄λ‹€ λ¨Όμ € ν˜ΈμΆœν•˜λ©΄ λͺ…μ‹œμ  μ—λŸ¬**: main() μ•ˆμ—μ„œ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜κ³  κ·Έ μ •μ˜κ°€ μ•„λž˜μ— μžˆμ„ 뿐 사전 선언이 μ—†μœΌλ©΄ 컴파일 μ—λŸ¬κ°€ λ°œμƒν•œλ‹€λŠ” 점이 직접 μ˜ˆμ‹œλ‘œ 확인됨 β€” C 챕터보닀 이 문제λ₯Ό 더 λͺ…ν™•νžˆ 강쑰함. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” ν•¨μˆ˜ 선언을 main() μœ„μ— 두고 μ •μ˜λ₯Ό μ•„λž˜μ— λ‘λŠ” ꡬ쑰가 컴파일 μ—λŸ¬λ₯Ό ν”Όν•˜λ©΄μ„œ μ½”λ“œ 가독성도 λ†’μ΄λŠ” μ‹€μ „ ν‘œμ€€ 관행이닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Forward-declaring a function above main() so it can be called before its full definition (C++): ```cpp // Function declaration void myFunction(); int main() { myFunction(); // works because of the forward declaration above return 0; } // Function definition void myFunction() { cout << "I just got executed!"; } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.85 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Memory Management New]], [[CPP Function Param]], [[C Functions]] - **μ°Έμ‘° λ§₯락:** ν•¨μˆ˜ μ„Ήμ…˜ 첫 챕터 β€” ν•¨μˆ˜ λ§€κ°œλ³€μˆ˜(Function Param) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Functions β€” https://www.w3schools.com/cpp/cpp_functions.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Functions" page (Astra wiki-curation, P-Reinforce v3.1 format).