--- id: javascript-function-definitions title: "JavaScript Function Definitions" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["function definitions", "function declaration", "function syntax", "defining functions", "local variables", "function intro"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "functions", "definitions"] raw_sources: ["https://www.w3schools.com/js/js_function_intro.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Function Definitions]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A function is defined with the `function` keyword, a name, a parenthesized parameter list, and a body β€” once defined it is a reusable block that runs only when called. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Functions are code blocks** β€” reusable code designed to perform a particular task; they execute when called or invoked. [S1] - **Definition syntax** β€” `function name(p1, p2, ...) { ... }`: keyword, name, comma-separated parameters in parentheses, body in braces. [S1] - **Why define functions** β€” to reuse code (write once, run many times) and organize code into manageable sections. [S1] - **Run on call** β€” the body does not run at definition time; it runs when the function is invoked. [S1] - **Local variables** β€” variables declared inside a function are accessible only within it, created when the function starts and deleted when it completes. [S1] - **Functions as variable values** β€” a function call can be used wherever a value is expected. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Name β†’ parameters β†’ body** β€” the standard declaration shape that makes a block callable by name. [S1] - **Parameterized reuse** β€” define once with parameters, call repeatedly with different arguments. [S1] - **Scope by definition** β€” placing a `let` inside the function body confines it to that function. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Functions are Code Blocks** β€” functions are reusable code blocks designed to perform a particular task; they execute when they are called or invoked. [S1] **Why Use Functions?** β€” functions let you reuse code (write once, run many times) and organize code into manageable sections. [S1] **JavaScript Function Syntax** β€” a function is defined with the `function` keyword, a name, parameters in parentheses, and a body in curly braces: [S1] ```javascript function name( p1, p2, ... ) { // code to be executed } ``` **What Does a Function Look Like?** β€” a basic definition: [S1] ```javascript function sayHello() { return "Hello World"; } ``` **Functions Run When You Call Them** β€” the defined function runs only when invoked, and the returned value can be stored: [S1] ```javascript function sayHello() { return "Hello World"; } let message = sayHello(); ``` A definition with parameters: [S1] ```javascript function multiply(a, b) { return a * b; } ``` **A Function Can Be Used Many Times** β€” the same definition is reused with different arguments: [S1] ```javascript function add(a, b) { return a + b; } let sum1 = add(5, 5); let sum2 = add(50, 50); ``` **Local Variables** β€” variables declared inside a function can only be accessed from within the function; they are created when the function starts and deleted when it completes: [S1] ```javascript // code here can NOT use carName function myFunction() { let carName = "Volvo"; // code here CAN use carName } // code here can NOT use carName ``` **Functions Used as Variables** β€” a function call can be used directly inside an expression: [S1] ```javascript let x = toCelsius(77); let text = "The temperature is " + x + " Celsius"; ``` And inlined directly: [S1] ```javascript let text = "The temperature is " + toCelsius(77) + " Celsius"; ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” defining `sayHello()`, `multiply(a, b)`, the reusable `add(a, b)`, the scoped `myFunction()`, and using `toCelsius(77)` as a value. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Define a function (language: JavaScript): ```javascript function name( p1, p2, ... ) { // code to be executed } ``` Define and reuse with parameters: ```javascript function add(a, b) { return a + b; } let sum1 = add(5, 5); let sum2 = add(50, 50); ``` Confine a variable to a function: ```javascript function myFunction() { let carName = "Volvo"; } ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Functions]], [[JavaScript Function Invocation]], [[JavaScript Function Parameters]], [[JavaScript Function Returns]] - **μ°Έμ‘° λ§₯락:** Referenced when learning how to declare and scope a reusable callable block. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Function Definitions β€” https://www.w3schools.com/js/js_function_intro.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Function Definitions" page (Astra wiki-curation, P-Reinforce v3.1 format).