Procedures and abstraction
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
What we'll do
- A procedure is a named block of code you can use again and again.
- In Python a procedure is made with
def. - This is part of Big Idea 3: building programs from reusable parts.
한국어
우리가 할 일
- 절차는 다시 사용할 수 있는 이름이 붙은 코드 블록입니다.
- Python에서 절차는
def로 만듭니다. - 이는 재사용 가능한 부분으로 프로그램을 구성하는 Big Idea 3의 일부입니다.
English
Make a procedure
- Start with
def, then a name, then(). - Put the code indented below.
- Call the procedure by writing its name with
().
한국어
절차 만들기
def로 시작한 후 이름, 그리고()을 넣습니다.- 코드를 들여쓰기하여 아래에 배치합니다.
- procedure 이름을
()와 함께 적어 호출합니다.
def greet():
print("Hello!")
greet()
greet()
English
Parameters pass data in
- A parameter is a value the procedure receives.
- Put the parameter name inside the
(). - Inside the procedure you use it like a variable.
한국어
매개변수는 데이터를 전달함
- 매개변수는 절차가 받는 값입니다.
- 매개변수 이름을
()안에 넣습니다. - 절차 내부에서는 변수처럼 사용합니다.
def greet(name):
print("Hello, " + name)
greet("Mia")
greet("Sam")
English
return gives a value back
returnsends a value out of the procedure.- The caller can store that value or use it.
- After
return, the procedure stops.
한국어
return가 값을 반환함
return는 절차를 나와서 값을 보냅니다.- 호출자는 그 값을 저장하거나 사용할 수 있습니다.
return이후 절차는 종료됩니다.
def square(n):
return n * n
answer = square(5)
print(answer)
English
Why procedures help: abstraction
- Abstraction means hiding the details behind a simple name.
- You call
square(5)and do not think about how it works inside. - This makes code shorter, clearer, and easy to reuse.
한국어
절차가 돕는 이유: 추상화
- 추상화란 간단한 이름 뒤에 디테일을 숨기는 것입니다.
square(5)를 호출하면 내부 작동 방식은 고려하지 않아도 됩니다.- 이는 코드를 짧고 명확하게 하며, 재사용하기 쉽게 만듭니다.
English
In AP CSP pseudocode
- The exam uses a pseudocode for procedures.
PROCEDUREis likedef;RETURNis likereturn.
한국어
AP CSP 가위수식에서
- 시험에서는 절차에 대해 가짜 언어(pseudocode)를 사용합니다.
PROCEDURE는def과 유사하며,RETURN는return과 유사합니다.
PROCEDURE square(n)
{
RETURN(n * n)
}
English
Common mistakes
- A procedure with a
returngives back a value; call it with(). - Abstraction hides the detail behind a simple name.
한국어
흔한 실수
return가 있는 절차는 값을 반환하며, 이를()로 호출합니다.- 추상화는 간단한 이름 뒤에 디테일을 숨깁니다.
English
Now you try
- Read each task to see the procedure name and what it must return.
- Press Check answer to test your code.
한국어
이제 직접 해보기
- 각 작업의 절차를 읽고, procedure 이름과 반환값을 확인하세요.
- Answer 확인 버튼을 눌러 코드를 테스트하세요.
Explore · 탐색하기
A procedure call
Calling a procedure passes arguments in and returns a value out.
Write a procedure square(n) that returns n multiplied by itself. Do not print inside it — use return.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Write a procedure area(width, height) with two parameters that returns the area (width times height).
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Write to_seconds(minutes, seconds) that returns the total number of seconds. (One minute is 60 seconds.) Then it combines a multiply step and an add step.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
The procedure double(x) is given. Call it with 21 and store the result in a variable named result.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.