Functions and Procedures
Functions and procedures address a recurring problem in programming: the same set of instructions may be needed in several places.
Why Reuse Matters
Functions and procedures address a recurring problem in programming: the same set of instructions may be needed in several places. Reuse means writing that set of instructions once, giving it a name, and calling the name whenever the instructions need to run. Instead of copying the same logic throughout a program, you keep one named set and use it repeatedly.
Repeated instructions create repeated maintenance. If a program checks whether a number is valid in five different places, copying the entire checking process five times means that each copy must be maintained separately. If the checking method improves, all five copies must be updated. Reuse places the instructions in one location, so the program is smaller, easier to read, and easier to change.
Reuse is not merely a way to save space. Naming an instruction set creates an abstraction: readers can understand what the named set does at a high level without tracing every internal step.
Following a Call
Execution does not always continue to the next instruction in the same location. When the program reaches a call to a named instruction set, execution moves to that instruction set's definition. The program completes all the steps in the definition and then returns to the instruction immediately after the call.
Tracing CheckValidity
A program reaches a location that says use the named instruction set CheckValidity. What execution path follows?
- 1Reach the call
Execution arrives at the location that calls CheckValidity.
- 2Jump to the definition
Execution moves to wherever CheckValidity is defined instead of immediately continuing to the next instruction.
- 3Complete the instructions
The program runs through all the steps belonging to CheckValidity.
- 4Return
After the instruction set finishes, execution returns to the instruction immediately after the call.
The call creates a temporary detour to the named instruction set, followed by a return to the original execution path.
One Definition, Many Calls
A single named instruction set can be invoked from many different locations. The instruction set remains in one place, while each call provides another route to it. This creates a one-to-many relationship: one definition is connected to many call sites.
The source example names a reusable instruction set ValidateEmail. A program can call it when a user signs up, when the user updates a profile, and when the user enters an email in a form. Each location invokes the same validation instructions. The context differs, but the named instruction set is shared.
Replacing Duplication
The main structural change produced by reuse is that repeated logic is replaced by one definition and several calls. The instructions are no longer scattered across the program. They exist in one place, while the locations that need them refer to that place by name.
A Number Check Used Five Times
A program needs to check whether a number is valid in five different places. Compare copying the checking process with naming it once and calling it where needed.
- 1Repeated approach
The entire checking process is written five separate times. Each copy takes space and must be maintained separately.
- 2Reusable approach
The checking process is written once and given a name. The five locations call that name instead of containing separate copies.
- 3Change the logic
If a better checking method is discovered, the reusable definition is changed in one location rather than updating five copies.
Reuse removes duplicated instructions and makes an improvement apply wherever the named instruction set is called.
Abstraction and Maintenance
A name hides the internal complexity of an instruction set. When a reader sees a call to ValidateEmail, the reader can understand the high-level action without tracing every validation step. The name acts as a container for the details.
This abstraction also makes a program more resilient to change. The details inside a named instruction set can change while the places that call it remain unchanged. The callers need to know the name and its high-level purpose; they do not need to know every internal detail.
Common Mistakes
Common mistake Thinking that a call duplicates the instruction set at the call site
What it looks likeCalling ValidateEmail from three locations does not require three separate copies of the validation instructions.
Why it failsThe purpose of reuse is that one named instruction set can be invoked from many locations.
Do this insteadTrack each location as a call to the same shared definition.
Common mistake Assuming execution simply continues to the next instruction after a call
What it looks likeAfter a call to CheckValidity, execution first moves to the CheckValidity definition.
Why it failsThe called instruction set must complete its steps before the original sequence continues.
Do this insteadTrace the path as call, definition, completion, and return.
Common mistake Updating only one copy of repeated logic
What it looks likeChanging one of five copied number-checking processes leaves the other four unchanged.
Why it failsSeparate copies can become inconsistent and require separate maintenance.
Do this insteadPlace the instructions in one named definition and call it from each required location.
Common mistake Focusing on internal steps when a high-level name is enough
What it looks likeA reader seeing Call ValidateEmail does not need to inspect every validation step to understand the program's high-level action.
Why it failsNaming creates an abstraction that makes the larger program easier to understand.
Do this insteadUse the named instruction set as the high-level unit of reasoning, then inspect its details when necessary.
Practice the Trace
A program calls ValidateEmail from a profile-update location. Describe the execution path in order, then explain what happens if the validation instructions are improved later.
Hints
- Start at the profile-update call site.
- Identify where execution goes before the next instruction runs.
- Include the return to the instruction after the call.
- Consider how one shared definition affects every caller.
What do you think happens?
A named instruction set is called from three different locations. If its definition is improved, how many definitions need to be changed?
Reveal answer
One
The reusable instructions exist in one definition. All three callers use that shared definition, so an improvement applies wherever the name is called.
Key Takeaways
- 1Reuse means writing a set of instructions once, naming it, and calling that name multiple times.
- 2When a call is reached, execution moves to the named definition, completes its steps, and returns to continue after the call.
- 3One named instruction set can serve many call sites, creating a one-to-many relationship.
- 4Reuse reduces duplication and makes programs easier to understand, maintain, and modify.
- 5Naming creates an abstraction that lets programmers reason about a task at a high level while keeping its detailed steps in one place.
At a glance
- Functions and procedures support reuse by giving a name to a set of instructions that can be called repeatedly.
- Execution temporarily moves to the named instruction set, completes its steps, and returns to the original sequence.
- Many call sites can share one definition, eliminating duplicated instructions.
- Centralizing instructions makes fixes and improvements apply consistently wherever the name is used.
- Naming instruction sets creates abstractions that help programmers manage complexity.