Welcome to Calling Procedures!

In this chapter, we are going to learn how to put our code to work efficiently. Imagine you had to explain how to make a peanut butter and jelly sandwich every single time someone was hungry. That would be exhausting! Instead, you just say, "Make a PB&J." In programming, that "shortcut" is called a procedure call. Let’s dive into how we use these shortcuts to manage complexity and make our programs smarter.


1. What is a Procedure?

A procedure is a named group of programming instructions that may have parameters and return values. You might also hear these called functions or methods in other programming languages, but for the AP CSP exam, we always call them procedures.

Procedures are the ultimate "reuse" tool. Instead of writing the same ten lines of code over and over, you put them in a procedure and "call" it whenever you need it.


2. The Anatomy of a Procedure Call

To use a procedure, you must "call" it. When you call a procedure, the program interrupts its current flow, jumps to the procedure's code, runs it, and then comes back to where it left off.

Parameters vs. Arguments

These two terms are often confused, but here is a simple way to remember the difference:

  • Parameters: These are the placeholders defined in the procedure header. They act like variables that only exist inside the procedure.
  • Arguments: These are the actual values you send to the procedure when you call it.

Analogy Time: Think of a procedure as a "Recipe for Smoothies."
The recipe says you need (fruit) and (liquid). Those are the parameters (the placeholders).
When you actually make the smoothie, you provide "Strawberries" and "Milk". Those are the arguments (the real stuff).

AP Reference Sheet Notation:
Definition: \(PROCEDURE \text{ procName(parameter1, parameter2, ...)}\)
The Call: \(procName(\text{arg1, arg2, ...})\)

Note: When the call is made, \(arg1\) is assigned to \(parameter1\), \(arg2\) to \(parameter2\), and so on.


3. Using Return Values

Some procedures just "do something" (like moving a robot), while others "give something back." This is handled by the RETURN statement.

When a procedure reaches a \(RETURN\) statement, it immediately stops what it's doing and sends a value back to the place where the procedure was called. You can then catch that value in a variable.

Example:
Imagine a procedure \(calculateTotal(price, tax)\).
Inside the procedure, it might have: \(RETURN(price + tax)\).
In your main program, you would call it like this:
\(finalAmount \leftarrow calculateTotal(20, 1.50)\)

Important Rule: As soon as a \(RETURN\) statement is executed, the procedure ends. Any code written after the \(RETURN\) statement inside that procedure will be ignored!


Quick Review: The Flow of a Call
  1. The program hits the procedure call.
  2. The arguments are assigned to the parameters.
  3. The code inside the procedure runs.
  4. If there is a \(RETURN\) statement, the value is sent back.
  5. The program continues on the next line after the call.

4. Procedural Abstraction: Managing Complexity

One of the most important concepts in Big Idea 3 is Procedural Abstraction. This is a fancy way of saying that you don't need to know how a procedure works to use it; you only need to know what it does.

Why is this helpful?

  • Hides Detail: You can use a procedure like \(calculateSquareRoot(x)\) without knowing the complex math inside it.
  • Reduces Duplication: If you find a bug in the procedure, you only have to fix it in one place (the definition), and every call to that procedure is automatically fixed.
  • Readability: It is much easier to read a program that says \(playGame()\) than one that has 500 lines of messy logic right in the middle of the screen.

5. Common Mistakes to Avoid

Don't worry if this seems tricky at first! Many students trip up on these specific areas:

  • Wrong Number of Arguments: If a procedure has three parameters, you must provide three arguments.
  • Order Matters: If a procedure is \(subtract(a, b)\), calling \(subtract(10, 2)\) is different than \(subtract(2, 10)\). The arguments match the parameters in the exact order they are listed.
  • Ignoring the Return Value: If a procedure returns a value but you don't assign it to a variable (using \(\leftarrow\)), that value is essentially "lost" into the void!

Key Takeaways

1. Calling: You call a procedure by using its name and providing the necessary arguments in parentheses.

2. Arguments vs. Parameters: Parameters are the names in the definition; arguments are the values passed in the call.

3. Return: The \(RETURN\) statement sends a value back and ends the procedure immediately.

4. Abstraction: Procedures help manage complexity by hiding details and allowing us to focus on the "big picture" of our program.


To learn more about how to create your own procedures from scratch, check out the next chapter: "Developing Procedures."