이 페이지는 최신 브라우저(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.
English
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.
English
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.

English
def square(n):
return n * n
answer = square(6)
print(answer) # 36
탐색하기
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).
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.
English
Now you try
- Write each function — it is then called for you in the check.
- Press Check answer to test your code.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Write a 함수(function) double(n) that 환원 n times 2.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Write a function is_even(n) that returns True when n is even, otherwise False.
출력을 보려면 '실행'을 클릭하세요.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Write a procedure print_stars(n) that prints a line of n star (*) characters, then call it with 4 so it prints ****.
출력을 보려면 '실행'을 클릭하세요.