Procedures, Abstraction, Libraries and Simulations
AP Computer Science Principles · Unit 3 — Algorithms and Programming
Procedures and procedural abstraction
A procedure is a named group of programming instructions that may have parameters and may return a value. Parameters are input variables of the procedure; arguments are the values supplied when the procedure is called.
PROCEDURE procName(parameter1, parameter2)
{
<block of statements>
RETURN(expression)
}
result ← procName(arg1, arg2)When the call runs, each parameter is assigned the value of the matching argument, the body executes, and RETURN(expression) immediately ends the procedure and sends the value back to the place of the call. Any statements after a RETURN that is reached are skipped, including the rest of a loop. A procedure that is never told to return produces no value; a procedure that returns from inside a loop stops at the first pass that reaches the RETURN.
Procedural abstraction means using a procedure by knowing what it does — its name, parameters and return value — without knowing how it does it. This manages complexity in three ways. A program is broken into smaller subproblems, each solved by a procedure (modularity). The same code is reused with different arguments instead of being copied, so a change made once applies everywhere. And the inside of a procedure can be rewritten — made faster, fixed — without changing any code that calls it, as long as its behaviour stays the same. Parameters are what make a procedure general: countAbove(list, limit) works for any list and any limit, while code with the number 30 written into it solves only one problem.
Libraries, APIs, random values and simulations
A software library contains procedures that can be used in creating new programs. An application program interface (API) is the specification of how the procedures in a library behave and can be used. Documentation for a library or API — what each procedure does, its parameters and its return value — is necessary to use it correctly. Libraries let programmers build on existing, tested code instead of writing everything from scratch.
RANDOM(a, b) generates and returns a random integer from a to b, inclusive, with each result equally likely. RANDOM(1, 6) behaves like a die: six outcomes, each with probability $\frac{1}{6}$. Two separate calls are independent. A condition such as RANDOM(1, 10) ≤ 3 is true with probability $\frac{3}{10}$. Each run of a program that uses random values may produce a different result, so a question may ask which outputs are possible rather than which output occurs.
A simulation is a program that imitates a real-world process. Simulations are abstractions: they keep the details that matter for the question and leave out others, using simplifying assumptions (every customer takes exactly 2 minutes; rain on one day is independent of the day before). They let us study situations that are too slow, expensive or dangerous to try for real, and they can be run many times with different inputs. Their results are only as trustworthy as their assumptions: a simulation built on a biased or oversimplified model gives confidently wrong answers. Running a simulation more times reduces the effect of chance, but it does not fix a wrong assumption.
Worked example: Xác suất từ RANDOM và lời gọi có RETURN sớm
(1) Find the probability that the segment displays "win".
a ← RANDOM(1, 4)
b ← RANDOM(1, 4)
IF(a > b)
{
DISPLAY("win")
}(2) What does DISPLAY(firstOver([3, 9, 4, 12], 5) + firstOver([1, 2], 5)) display?
PROCEDURE firstOver(list, k)
{
FOR EACH v IN list
{
IF(v > k)
{
RETURN(v)
}
}
RETURN(-1)
}Solution
(1) Có $4 \times 4 = 16$ cặp (a, b) đồng khả năng. Đếm cặp có a > b: a = 2 có 1 cặp, a = 3 có 2, a = 4 có 3, tổng 6. Xác suất $\frac{6}{16}=\frac{3}{8}$. Cách nhanh: 4 cặp bằng nhau, 12 cặp còn lại chia đều hai phía a > b và a < b.
(2) Lời gọi thứ nhất: v = 3 không lớn hơn 5; v = 9 lớn hơn 5 nên RETURN(9) ngay, không xét 4 và 12. Lời gọi thứ hai: không phần tử nào lớn hơn 5, vòng lặp chạy hết, RETURN(−1). In $9 + (-1) = $ 8. Ai đọc thủ tục là “trả về phần tử lớn nhất lớn hơn k” sẽ ra 12 − 1 = 11 — sai vì RETURN kết thúc thủ tục ở lần đầu điều kiện đúng.
Common mistake
Bẫy RETURN và bẫy RANDOM. RETURN trong vòng lặp không “ghi nhận rồi chạy tiếp” — nó thoát cả thủ tục. Với RANDOM:RANDOM(1, 6) + RANDOM(1, 6) không đồng khả năng từ 2 đến 12 (tổng 7 có 6 cách, tổng 2 chỉ 1 cách), và RANDOM(0, 5) có 6 kết quả chứ không phải 5. Bẫy mô phỏng: nghĩ chạy 1 triệu lần là sửa được giả định sai.