Procedures & 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
A function is a named block
- A function is a named block of code you can run again and again.
- Define it once with
def, then call it by name. - This avoids repeating yourself and keeps programs tidy.
한국어
함수란 명명된 블록이다
- 함수는 다시 반복해서 실행할 수 있는 명명된 코드 블록입니다.
def로 한 번 정의한 뒤, 이름으로 호출하십시오.- 이를 통해 코드의 중복을 방지하고 프로그램을 깔끔하게 유지할 수 있습니다.
def greet():
print("Hello!")
greet()
greet()
English
Parameters pass data in
- A parameter is a value the function receives.
- Put it in the brackets, and pass an argument when you call.
- Keep the number of parameters small so the function stays easy to read.
한국어
매개변수는 데이터를 전달함
- 매개변수는 함수가受け取는 값입니다.
- 괄호 안에 입력하고, 호출할 때는 인자를 전달하십시오.
- 함수가 쉽게 읽히도록 매개변수의 개수를 적게 유지하십시오.
def greet(name):
print("Hello, " + name)
greet("Ada")
greet("Sam")
English
Functions return a value
- A function hands a value back with
return. - A block that only does something and returns nothing is a procedure.
한국어
함수는 값을 반환한다
- 함수는
return를 사용하여 값을 반환합니다. - 어떤 작업만 수행하고 값을 반환하지 않는 블록을 절차라고 합니다.
def square(n):
return n * n
answer = square(6)
print(answer) # 36
Explore · 탐색하기
Call and return
Calling runs the function's body; return hands a value back.
English
In the exam: PROCEDURE and FUNCTION
- Pseudocode separates a
PROCEDURE(no result) from aFUNCTION(returns a value).
한국어
시험에서: 절차(PROCEDURE)와 함수(FUNCTION)
- 가위코드(pseudocode)는
PROCEDURE(결과 없음)과FUNCTION(값을 반환)를 구분합니다.
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
English
Common mistakes
- A function returns a value with
return; a procedure just does a job. - Call it with
(). - Parameters receive the arguments in order.
한국어
흔한 실수
- 함수는
return를 사용하여 값을 반환하고, 절차는 단순히 작업을 수행할 뿐입니다. - 이를
()로 호출합니다. - 매개변수는 인자를 순서대로受け取합니다.
English
Now you try
- Write each function — it is then called for you in the check.
- Press Check answer to test your code.
한국어
이제 직접 해보기
- 각 함수를 작성하십시오. 그러면 검증 단계에서 자동으로 호출됩니다.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
Write a function double(n) that returns n times 2.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Write a function is_even(n) that returns True when n is even, otherwise False.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Write a procedure print_stars(n) that prints a line of n star (*) characters, then call it with 4 so it prints ****.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.