Concepts / Functions and Procedures

Functions and Procedures

Reuse means writing a set of instructions once, naming it, and calling that name multiple times throughout a program

  • Programming

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.

callreturnProgram locationCall CheckValidityCheckValidityComplete all stepsNext instructionContinue where the callleft off
What happens when execution reaches a call to a named instruction set?

Tracing CheckValidity

A program reaches a location that says use the named instruction set CheckValidity. What execution path follows?

Reach the call: Execution arrives at the location that calls CheckValidity.

Jump to the definition: Execution moves to wherever CheckValidity is defined instead of immediately continuing to the next instruction.

Complete the instructions: The program runs through all the steps belonging to CheckValidity.

Return: 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.

invokesinvokesinvokesUser signupCall ValidateEmailProfile updateCall ValidateEmailForm entryCall ValidateEmailValidateEmailOne shared instruction set
How are several call sites connected to one shared set of instructions?

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.

invokesinvokesinvokesCheck instructionsCopy 1CheckValidityOne definitionCheck instructionsCopy 2Call siteLocation 1Check instructionsCopy 3Call siteLocation 2Call siteLocation 3
What changes when repeated instructions are replaced by one named instruction set?

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.

Repeated approach: The entire checking process is written five separate times. Each copy takes space and must be maintained separately.

Reusable approach: The checking process is written once and given a name. The five locations call that name instead of containing separate copies.

Change 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

  • Thinking that a call duplicates the instruction set at the call site

    The purpose of reuse is that one named instruction set can be invoked from many locations.

    Fix: Track each location as a call to the same shared definition.

  • Assuming execution simply continues to the next instruction after a call

    The called instruction set must complete its steps before the original sequence continues.

    Fix: Trace the path as call, definition, completion, and return.

  • Updating only one copy of repeated logic

    Separate copies can become inconsistent and require separate maintenance.

    Fix: Place the instructions in one named definition and call it from each required location.

  • Focusing on internal steps when a high-level name is enough

    Naming creates an abstraction that makes the larger program easier to understand.

    Fix: Use the named instruction set as the high-level unit of reasoning, then inspect its details when necessary.

Practice the Trace

EASY

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?

  • One
  • Three
  • One for every future call
Reveal answer

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

  1. Reuse means writing a set of instructions once, naming it, and calling that name multiple times.
  2. When a call is reached, execution moves to the named definition, completes its steps, and returns to continue after the call.
  3. One named instruction set can serve many call sites, creating a one-to-many relationship.
  4. Reuse reduces duplication and makes programs easier to understand, maintain, and modify.
  5. Naming creates an abstraction that lets programmers reason about a task at a high level while keeping its detailed steps in one place.

Key Takeaways

  • 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.