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
Name a block of code
- A function is named code you can use again and again.
- Make one with
def name():and an indented body. - "Calling" the function by its name runs that code.
한국어
코드 블록에 이름 붙이기
- **함수(function)**는 다시 사용할 수 있는 이름이 붙은 코드입니다.
def name():와 들여쓴 바디(body)로 하나 만드십시오.- 이름으로 함수를 '호칭(calling)'하면 해당 코드가 실행됩니다.
English
Give it inputs (parameters)
- The names in the brackets are parameters — the function's inputs.
- When you call it, you pass in arguments (the real values).
- A function can take more than one parameter.
한국어
입력값(파라미터) 제공하기
- 괄호 안의 이름들은 파라미터입니다 — 함수의 입력값입니다.
- 호출할 때 인자(argument) (실제 값)를 전달합니다.
- 함수는 여러 개의 파라미터를 받을 수 있습니다.
def shout(word):
print(word.upper() + "!")
shout("hello")
English
Send back a result (return)
returnsends a value back to whoever called the function.- You can store that value or use it in an expression.
returnalso ends the function straight away.
한국어
결과(return) 보내기
return가 호출한 사람에게 반환되는 값을 보냅니다.- 그 값을 저장하거나 식(expression)에서 사용할 수 있습니다.
return도 함수를 즉시 종료합니다.
def double(x):
return x * 2
result = double(5)
print(result)
English
return vs print
printonly shows something on the screen.returngives a value back that the rest of your code can use.- A function with no
returngives backNone.
한국어
return과 print의 차이
print은 화면에 값만 표시할 뿐입니다.return은 나머지 코드에서 사용할 수 있는 값을 반환합니다.return이 없는 함수는None을 반환합니다.
English
In Cambridge pseudocode
- A
FUNCTIONreturns a value; aPROCEDUREjust does an action.
한국어
캐미지아 가위코드에서
FUNCTION은 값을 반환하지만,PROCEDURE은 단순히 작업을 수행할 뿐입니다.
FUNCTION Add(A : INTEGER, B : INTEGER) RETURNS INTEGER
RETURN A + B
ENDFUNCTION
PROCEDURE Greet(Name : STRING)
OUTPUT "Hello, ", Name
ENDPROCEDURE
English
Common mistakes
- A function returns
Noneunless you writereturn; printing is not the same as returning. - Run a function with
():greet(), notgreet. - Define a function before you call it.
한국어
흔한 실수
- 함수는
None을 반환하지만, 명시적으로return을 작성하지 않으면 반환되지 않습니다. 출력은 반환과 동일하지 않습니다. - 함수를 호출할 때는
()을 사용하십시오:greet()이 아니라greet을 사용하지 마십시오. - 함수를 호출하기 전에 먼저 정의해야 합니다.
English
Now you try
- Define each function, then the checks will call it for you.
- Use
returnto send the answer back (do notprintit).
한국어
이제 직접 해보기
- 각 함수를 정의한 후, 체크 기능(검증)이 자동으로 해당 함수를 호출합니다.
- 정답을 반환하기 위해
return을 사용하십시오. (print하지 마십시오.)
Explore · 탐색하기
Calling a function
A call pushes a frame with its arguments; return pops it.
Write a function add(a, b) that returns the sum of a and b.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Write a function greet(name) that returns the string Hello, then the name then !. So greet("Sam") returns Hello, Sam!.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Write a function biggest(a, b) that returns the larger of the two values. If they are equal, return either one.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Write is_palindrome(word) that returns True when the word reads the same backwards (like racecar), otherwise False.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.