AP Computer Science Principles · Unit 3 lecture · Part 11 of 14 · 11 min

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

Mr. Thompkins
Every big program is built from small named pieces. You've already used some: DISPLAY, INPUT, APPEND. Now you'll learn how they work, and how to write your own. This is also the heart of your Create task.

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.

main programDISPLAY("Start")greet("Ana")DISPLAY("End")PROCEDURE greet(name)DISPLAY("Hi")DISPLAY(name)1. jump2. come backoutput: Start Hi Ana End
A call jumps into the procedure, runs it, and comes back to the next line of the main program.
?Wait, what's the difference between DISPLAY inside a procedure and RETURN?
DISPLAY shows a value on the screen. RETURN sends a value back to the place the procedure was called, where it can be stored, displayed or used in an expression, and it ends the procedure immediately. A procedure that only DISPLAYs gives nothing back to use.
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
  1. line 1 Three parameters: x, low and high
  2. line 5-9 RETURN sends a value back and ends the procedure
  3. line 13-15 Three calls with different arguments
output › 10 0 7
clamp keeps x inside the range low to high.
TermWhere you see itIn clamp(15, 0, 10)
ParameterIn the PROCEDURE line: input variablesx, low, high
ArgumentIn the call: the actual values15, 0, 10
Return valueWhat RETURN sends back to the call10
Parameters, arguments and return values, for one call.

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}
  1. line 7 Ends the whole procedure, loop and all
  2. line 10 Reached only if no negative was found
firstNeg([3, -2, 5, -8]) returns -2; the -8 is never looked at.
?Wait, can the result of one call be an argument to another?
Yes. Evaluate the inner call first, then use its return value as the argument of the outer call, just like nested string procedures. clamp(clamp(12, 0, 10) * 2, 5, 15) becomes clamp(20, 5, 15), which is 15.

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)
Three copies of the same calculation.
1PROCEDURE around(r)2{3  RETURN(r * 3.14 * 2)14}5DISPLAY(around(3))6DISPLAY(around(5))7DISPLAY(around(8))
  1. line 3 The shared work, written once, with r as the part that changes
One procedure with a parameter does all three.
Pause and think
In around(r), what is the parameter, and what are the arguments in the three calls?
I thought about it. Show me
The parameter is r. The arguments are 3, 5 and 8, one per call. Each call runs the same code with r set to a different argument.
Trace calls and returns
Using clamp, what is displayed by DISPLAY(clamp(clamp(12, 0, 10) * 2, 5, 15))? Then name which RETURN runs in each call.
  1. 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.
  2. The outer argument becomes 10 * 2 = 20.
  3. 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.
  4. DISPLAY shows 15.

Answer: 15. Both calls end at RETURN(high).

Stuck? Get a hint
  1. Start with clamp(12, 0, 10). Is 12 above 10?
  2. Multiply that result by 2.
  3. Then clamp 20 between 5 and 15.
Check yourself
What is displayed when this code segment runs?
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)))
  1. A10 3
  2. B10 6
  3. C4 3
  4. 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.

Check yourself
A programmer replaces six nearly identical blocks of drawing code with one procedure drawBadge(size, color). Which is the best explanation of the benefit?
  1. AThe program now uses less memory in every case.
  2. 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.
  3. CProcedures cannot contain errors.
  4. 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.