3.12 and 3.13 Calling and developing proceduresName it once, use it everywhere

A procedure is a named group of programming instructions that may have parameters and return values. Other languages call procedures methods or functions. Parameters are input variables of a procedure, listed in its definition. Arguments are the actual values given to the parameters when the procedure is called.
A procedure call interrupts the normal top-to-bottom flow. The program jumps into the procedure, runs its statements with the parameters set to the arguments, and then returns to the point immediately after the call. It returns when it reaches the end of the procedure, or as soon as it runs a RETURN statement.
?Wait, what's the difference between DISPLAY inside a procedure and RETURN?
1PROCEDURE clamp(x, low, high)12{3 IF (x < low)4 {5 RETURN(low)26 }7 IF (x > high)8 {9 RETURN(high)210 }11 RETURN(x)12}13DISPLAY(clamp(15, 0, 10))314DISPLAY(clamp(-3, 0, 10))315DISPLAY(clamp(7, 0, 10))3
- line 1 Three parameters: x, low and high
- line 5-9 RETURN sends a value back and ends the procedure
- line 13-15 Three calls with different arguments
| Term | Where you see it | In clamp(15, 0, 10) |
|---|---|---|
| Parameter | In the PROCEDURE line: input variables | x, low, high |
| Argument | In the call: the actual values | 15, 0, 10 |
| Return value | What RETURN sends back to the call | 10 |
RETURN(expression) ends the procedure right away, even in the middle of a loop. Any code after it in the procedure doesn't run for that call. firstNeg uses that on purpose: as soon as it sees a negative number, it returns it, and the loop never finishes. The RETURN(0) at the bottom only runs if the loop ends without finding one.
1PROCEDURE firstNeg(nums)2{3 FOR EACH n IN nums4 {5 IF (n < 0)6 {7 RETURN(n)18 }9 }10 RETURN(0)211}
- line 7 Ends the whole procedure, loop and all
- line 10 Reached only if no negative was found
?Wait, can the result of one call be an argument to another?
Procedural abstraction
Procedural abstraction gives a process a name and lets you use the procedure knowing only what it does, not how it does it. You've used APPEND hundreds of times without knowing how it's built. That lets a big problem be solved from the solutions to smaller subproblems. Dividing a program into separate subprograms is called modularity.
Procedures with parameters also generalize code. When the same steps appear several times with different numbers, pull the shared steps into one procedure and make the part that changes a parameter. Then a fix happens in one place, the code is shorter and easier to read, and the program can do the job for any value. And as long as the procedure still does the same thing, its programmer can change how it works inside without anyone who calls it needing to change their code.
1DISPLAY(3 * 3.14 * 2)2DISPLAY(5 * 3.14 * 2)3DISPLAY(8 * 3.14 * 2)
1PROCEDURE around(r)2{3 RETURN(r * 3.14 * 2)14}5DISPLAY(around(3))6DISPLAY(around(5))7DISPLAY(around(8))
- line 3 The shared work, written once, with r as the part that changes
I thought about it. Show me
- Inner call: clamp(12, 0, 10). 12 < 0 is false; 12 > 10 is true, so it runs RETURN(high) and returns 10.
Why?
Evaluate the innermost call first. - The outer argument becomes 10 * 2 = 20.
- Outer call: clamp(20, 5, 15). 20 < 5 is false; 20 > 15 is true, so RETURN(high) again, returning 15.
Why?
Each call has its own x, low and high, set from its own arguments. - DISPLAY shows 15.
Answer: 15. Both calls end at RETURN(high).
Stuck? Get a hint
- Start with clamp(12, 0, 10). Is 12 above 10?
- Multiply that result by 2.
- Then clamp 20 between 5 and 15.
PROCEDURE tri(n)
{
total ← 0
k ← 1
REPEAT n TIMES
{
total ← total + k
k ← k + 1
}
RETURN(total)
}
DISPLAY(tri(4))
DISPLAY(tri(tri(2)))- A10 3
- B10 6
- C4 3
- D10 10
Show the answer
B. tri(4) adds 1 + 2 + 3 + 4 = 10. tri(2) is 1 + 2 = 3, so tri(tri(2)) is tri(3) = 1 + 2 + 3 = 6. 10 3 stops after the inner call.
- AThe program now uses less memory in every case.
- BThe procedure generalizes the task: one copy of the code handles every size and color, so it is easier to read and a fix happens in one place.
- CProcedures cannot contain errors.
- DThe program no longer needs parameters.
Show the answer
B. That's procedural abstraction: shared steps written once, with parameters for what changes, improving readability and making changes easier. Procedures can still have bugs; they just get fixed once.
- A procedure is a named group of instructions. Parameters are its input variables; arguments are the values in a call.
- A call jumps into the procedure and returns to the next statement. RETURN sends back a value and ends the procedure at once.
- Procedural abstraction: use a procedure knowing what it does, not how. Modularity: split a program into subprograms.
- Parameters generalize code and remove duplication; the inside can change without changing the callers.