Concepts / Functions and Procedures

Functions and Procedures

Functions and procedures address a recurring problem in programming: the same set of instructions may be needed in several places.

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

Key point

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.

Program locationCall CheckValidityCheckValidityComplete all stepsNext instructionContinue where the callleft offcallreturn
What happens when execution reaches a call to a named instruction set?
Worked example

Tracing CheckValidity

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

  1. 1
    Reach the call

    Execution arrives at the location that calls CheckValidity.

  2. 2
    Jump to the definition

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

  3. 3
    Complete the instructions

    The program runs through all the steps belonging to CheckValidity.

  4. 4
    Return

    After the instruction set finishes, execution returns to the instruction immediately after the call.

Result

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.

User signupCall ValidateEmailProfile updateCall ValidateEmailForm entryCall ValidateEmailValidateEmailOne shared instruction setinvokesinvokesinvokes
How are several call sites connected to one shared set of instructions?
In the real world

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.

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

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.

  1. 1
    Repeated approach

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

  2. 2
    Reusable approach

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

  3. 3
    Change the logic

    If a better checking method is discovered, the reusable definition is changed in one location rather than updating five copies.

Result

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 like

    Calling ValidateEmail from three locations does not require three separate copies of the validation instructions.

    Why it fails

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

    Do this instead

    Track 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 like

    After a call to CheckValidity, execution first moves to the CheckValidity definition.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Updating only one copy of repeated logic

    What it looks like

    Changing one of five copied number-checking processes leaves the other four unchanged.

    Why it fails

    Separate copies can become inconsistent and require separate maintenance.

    Do this instead

    Place 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 like

    A reader seeing Call ValidateEmail does not need to inspect every validation step to understand the program's high-level action.

    Why it fails

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

    Do this instead

    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. 1
    Reuse means writing a set of instructions once, naming it, and calling that name multiple times.
  2. 2
    When a call is reached, execution moves to the named definition, completes its steps, and returns to continue after the call.
  3. 3
    One named instruction set can serve many call sites, creating a one-to-many relationship.
  4. 4
    Reuse reduces duplication and makes programs easier to understand, maintain, and modify.
  5. 5
    Naming 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.

Related concepts

More concepts starting with F