Functions · 함수
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
Functions
- A function is a named block of code you can run again and again.
function name(params) { ... return value; }. Theparamsare the inputs.returnhands a value back to whoever called the function.
한국어
함수
- 함수는 다시 반복해서 실행할 수 있는 명명된 코드 블록입니다.
function name(params) { ... return value; }.params은 입력값입니다.return는 함수를 호출한 사람에게 값을 반환합니다.
function square(n) {
return n * n;
}
console.log(square(6)); // → 36
English
Functions that just do something
- A function can do something (like print) instead of returning a value.
- Call it by writing its name and brackets:
greet("Ada").
한국어
단순히 동작을 수행하는 함수
- 함수는 값을 반환하는 대신 무언가를 실행할 수 있습니다(예: 출력).
- 함수 이름을 쓰고 괄호를 붙여 호출합니다:
greet("Ada").
function greet(name) {
console.log("Hi, " + name); // → Hi, Ada
}
greet("Ada");
Explore · 탐색하기
Call and return · 호출 및 반환
Each call runs the body with its own input, then return hands a value back. · 매번 호출될 때마다 본문이 고유한 입력으로 실행된 후, return이 값을 다시 반환합니다.
English
Watch out
- The names in the brackets (
n,name) are parameters — they only exist inside the function. - A function with no
returngives backundefined. Addreturnwhen you want an answer out.
한국어
주의사항
- 괄호 안의 이름들(
n,name)은 매개변수입니다. 이들은 함수 내부에서만 존재합니다. return가 없으면 함수는undefined을 반환합니다. 결과를 얻으려면return을 추가하세요.
English
Common mistakes
returngives back a value; without it a function returnsundefined.- Call the function with
().
한국어
흔한 실수
return이 있으면 값을 반환하지만, 없으면 함수는undefined을 반환합니다.()를 인자로 사용하여 함수를 호출하세요.
English
Now you try
- Write each function — it is called for you in the check.
- Press Check answer to test it.
한국어
이제 직접 해보기
- 각 함수를 작성하세요 — 체크에서 자동으로 호출됩니다.
- 답안 확인을 눌러 테스트해 보십시오.
Write a function double(n) that returns n times 2. · double(n)이라는 함수를 작성하여 반환값을 n × 2로 만듭니다.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Write a function add(a, b) that returns a + b. · add(a, b)이라는 함수를 작성하여 a + b을 반환합니다.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Write a function greet(name) that logs Hi, then the name, then call greet("Ada"). Output should be Hi, Ada. · greet(name)이라는 함수를 작성하여 Hi, 과 이름을 출력한 후, greet("Ada")을 호출합니다. 출력은 Hi, Ada이어야 합니다.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.