ν•¨μˆ˜_part.1😊

Bonnie RyuΒ·2020λ…„ 8μ›” 17일
0
post-custom-banner

LeetCode λ¬Έμ œν’€λ‹€κ°€ 와λ₯΄λ₯΄λ₯΄λ₯΄...πŸ€•
ν•¨μˆ˜μ— λŒ€ν•΄ μ–΄λ””κΉŒμ§€ μ•Œκ³ μžˆλ‚˜...

ν•¨μˆ˜κ³΅λΆ€ λ‹€μ‹œν•˜κΈ°.. μ˜€λŠ˜λ„ w3shoolμ„ μƒλ‹˜κ³ΌπŸ˜Ά

μ˜€λŠ˜λ°€μ€ μš•μ‹¬λΆ€λ¦¬μ§€ 말고 w3s만 ν™•μ‹€ν•˜κ²Œ μ •λ¦¬ν•˜λ €κ³  ν•œλ‹€.

ν•¨μˆ˜(function)

  • A JavaScript function is a block of code designed to perform a particular task.
    JavaScript ν•¨μˆ˜λŠ” νŠΉμ • μž‘μ—…μ„ μˆ˜ν–‰ν•˜λ„λ‘ μ„€κ³„λœ μ½”λ“œ 블둝이닀.
  • A JavaScript function is executed when "something" invokes it (calls it).JavaScript ν•¨μˆ˜λŠ” "무언가"κ°€ 호좜 (호좜) ν•  λ•Œ μ‹€ν–‰λœλ‹€.

Function Syntax (ν•¨μˆ˜κ΅¬μ‘°)

function ν•¨μˆ˜μ΄λ¦„ () {}

ν•¨μˆ˜ μ΄λ¦„μ—λŠ” 문자, 숫자, _ 그리고 $둜 λͺ…λͺ…κ°€λŠ₯ (like λ³€μˆ˜λͺ…)
( )μ•ˆμ—λŠ” ','둜 κ΅¬λΆ„ν•˜λŠ” λ§€κ°œλ³€μˆ˜(parameter)κ°€ 포함될 수 μžˆλ‹€.
{ }μ•ˆμ—λŠ” ν•¨μˆ˜μ— μ˜ν•΄ 싀행될 μ½”λ“œκ°€ λ“€μ–΄κ°„λ‹€.

Function arguments are the values received by the function when it is invoked. μΈμˆ˜λŠ” ν•¨μˆ˜κ°€ ν˜ΈμΆœλ λ•Œ λ°›λŠ” 값이닀.

Inside the function, the arguments (the parameters) behave as local variables.ν•¨μˆ˜ λ‚΄μ—μ„œ 인수 (맀개 λ³€μˆ˜)λŠ” 지역 λ³€μˆ˜λ‘œ μž‘λ™ν•œλ‹€.

Function Invocation (ν•¨μˆ˜ν˜ΈμΆœ)

The code inside the function will execute when "something" invokes (calls) the function. ν•¨μˆ˜ λ‚΄λΆ€μ˜ μ½”λ“œλŠ” μ–΄λ–€ν•¨μˆ˜λ₯Ό 호좜 ν•  λ•Œ μ‹€ν–‰λœλ‹€.

  • When an event occurs (when a user clicks a button) 이벀트 λ°œμƒμ‹œ (μ‚¬μš©μžκ°€ λ²„νŠΌ ν΄λ¦­μ‹œ)
  • When it is invoked (called) from JavaScript code JavaScript μ½”λ“œμ—μ„œ 호좜 (호좜)λ˜λŠ” 경우
  • Functions often compute a return value. The return value is "returned" back to the "caller" ν•¨μˆ˜λŠ” λ°˜ν™˜ 값을 κ³„μ‚°ν•œλ‹€. λ°˜ν™˜ 값은 "λ°œμ‹ μž"μ—κ²Œ λ‹€μ‹œ "λ°˜ν™˜"λœλ‹€.
//ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜κ³  λ°˜ν™˜ 값은 x에 λ‹€μ‹œ λ°˜ν™˜λœλ‹€. 
const x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;

function myFunction(a, b) {
  return a * b;
}
  • Accessing a function without () will return the function object instead of the function result.()없이 ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λ©΄ ν•¨μˆ˜ κ²°κ³Ό λŒ€μ‹  ν•¨μˆ˜ 객체가 λ°˜ν™˜ν•œλ‹€.
function toCelsius(f) {
  return (5/9) * (f-32);
}
document.getElementById("demo").innerHTML = toCelsius;
//function toCelsius(f) { return (5/9) * (f-32); }

Functions Used as Variable Values(ν•¨μˆ˜ λ¦¬ν„°λŸ΄λ‘œ μ‚¬μš©)

Functions can be used the same way as you use variables, in all types of formulas, assignments, and calculations.ν•¨μˆ˜λŠ” λͺ¨λ“  μœ ν˜•μ˜ 곡식, ν• λ‹Ή 및 κ³„μ‚°μ—μ„œ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” 것과 λ™μΌν•œ λ°©μ‹μœΌλ‘œ μ‚¬μš©ν•  수 μžˆλ‹€.
= λ³€μˆ˜λ₯Ό μ‚¬μš©ν•  λ•Œμ™€ 같이 ν•¨μˆ˜λ₯Ό λ³€μˆ˜μ— ν• λ‹Ήν•œ ν›„ ν˜ΈμΆœν•  수 μžˆλ‹€.

ν•¨μˆ˜μ˜ λ°˜ν™˜ 값을 μ €μž₯ν•˜κΈ° μœ„ν•΄ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” λŒ€μ‹  :

const x = toCelsius(77);
const text = "The temperature is " + x + " Celsius";

ν•¨μˆ˜λ₯Ό λ³€μˆ˜ κ°’μœΌλ‘œ 직접 μ‚¬μš©ν•  수 μžˆλ‹€.

var text = "The temperature is " + toCelsius(77) + " Celsius";

Global Variables(μ „μ—­λ³€μˆ˜)

  • μ§€μ—­λ³€μˆ˜μ— μ•‘μ„ΈμŠ€ ν•  수 μžˆλ‹€.

Local Variables(μ§€μ—­λ³€μˆ˜)

  • 지역 λ³€μˆ˜λŠ” ν•¨μˆ˜ λ‚΄μ—μ„œλ§Œ μ•‘μ„ΈμŠ€ ν•  수 μžˆλ‹€.
  • 지역 λ³€μˆ˜λŠ” ν•¨μˆ˜ λ‚΄μ—μ„œλ§Œ μΈμ‹λ˜κΈ° λ•Œλ¬Έμ— 같은 μ΄λ¦„μ˜ λ³€μˆ˜λ₯Ό λ‹€λ₯Έ ν•¨μˆ˜μ—μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.
  • 지역 λ³€μˆ˜λŠ” ν•¨μˆ˜κ°€ μ‹œμž‘λ  λ•Œ μƒμ„±λ˜κ³  ν•¨μˆ˜κ°€ μ™„λ£Œλ˜λ©΄ μ‚­μ œλœλ‹€.
profile
Ryuwisdom
post-custom-banner

0개의 λŒ“κΈ€