Concepts / Functions and Routines in Java

Functions and Routines in Java

An API is a specification of the routines a toolbox or device makes available, the parameters each one takes, and the task each one performs.

  • Programming

The Question Behind Ready-Made Routines

Most useful programs are assembled from pieces that someone else wrote. A programmer who needs to sort records, draw a window, or solve a complicated equation rarely builds that machinery from nothing. Instead, they use a collection of ready-made routines and call them when needed. That raises an immediate practical question: if you did not write the routines, and you have no wish to study how they work internally, what do you actually need to know in order to use them correctly?

What do you think happens?

Before calling a routine you did not write, which facts do you need in order to use it correctly?

  • Its exact internal steps, line by line

  • Its name, the inputs it expects, and the task it carries out

  • The layout of the program's memory while it runs

  • Nothing, because you must read its source code first

Reveal answer
Answer:

Its name, the inputs it expects, and the task it carries out, along with the outcome you can expect.

The source explains that you need a description of the outside of the collection, not the inside. The description tells you what is available, what information each routine expects you to hand over, and what outcome you can expect. Keeping how something is done separate from what it does is the central idea behind APIs and abstraction.

This article develops that answer. The short version is that the routines you call come with a published outside, and that outside is what you must understand. The inside, meaning the instructions that do the actual work, can stay hidden without getting in your way.

Tracing a Call Across the Boundary

Start with a physical picture. Imagine you are handed a sealed box of tools. You can see a labeled panel on its front with a few buttons and a slot for input. You press a button, feed in material through the slot, and something comes out the other side. You never see the gears or circuits that do the work. Nothing about your task requires you to see them. The panel is the only part you have to understand.

Now trace the same situation through a software toolbox. Your program holds a reference to a collection of routines. Before you call any of them, the only facts available to you are the names of the routines, the inputs each one takes, and the task each one carries out. Everything the routine does between receiving those inputs and returning a result is invisible at the point where you call it. Keep that boundary in mind, because the rest of this article depends on it.

Programmer's codemakes the callAPI specificationname, inputs, task, resultRoutineimplementationhidden workingscalls throughkeeps behind
Which parts of a toolbox are visible to the programmer through the API, and which sit behind that boundary as hidden implementation?

Read the diagram from top to bottom. The programmer's code never touches the implementation directly. It reaches the workings only through the specification, and the workings sit behind that specification. The specification is the surface you have to understand. The implementation is the part that can change without changing how you call the routine.

What an API Specifies

Definition

An API is a specification of the routines a toolbox or device makes available, the parameters each one takes, and the task each one performs.

The API is the abstraction through which a programmer reaches a toolbox's functionality, which makes it the point of contact that users must understand. Each entry in an API tells you three things. It tells you the routine's name, which is how you refer to it. It tells you the parameters, which are the information you must hand over. It tells you the task, which is what the routine accomplishes. The specification does not show how the task is carried out.

  • Which routines the toolbox or device makes available
  • The parameters each routine takes, meaning the inputs it expects
  • The task each routine performs, meaning the outcome you can expect
  • What is deliberately left out: the internal steps used to produce that outcome
Worked example

Reading a routine description

A toolbox description says: a routine named average takes a list of numbers and returns their mean. Identify what the API exposes and what it hides.

  1. 1
    Find the name

    The name average is what the caller uses to refer to the routine. It is exposed.

  2. 2
    Find the parameter

    The routine takes a list of numbers. That tells the caller what to hand over, so the input requirement is exposed.

  3. 3
    Find the task and result

    The task is to compute the mean, and the result is a single number. The caller can expect that outcome, so it is exposed.

  4. 4
    Identify what is hidden

    The description says nothing about how the mean is computed, such as the order of additions or any special handling inside the routine. Those details are hidden implementation.

Result

The API exposes the name, the list input, and the mean result. The method of computing the mean stays behind the boundary.

Abstraction: Exposing Only What Is Needed

Abstraction is the practice of exposing only the information the outside world needs while hiding implementation details behind the interface. The word describes a deliberate choice about what crosses the boundary. A good interface gives the user enough to call the routine correctly and to predict its result, and nothing more.

Keeping the question of how something is done separate from the question of what it does is the central idea behind APIs and abstraction. A user asks what a routine does and what it needs. The implementer answers how it does the work. Because those questions are kept apart, the user does not have to study the machinery to use it, and the implementer is free to work on the machinery without changing the description the user relies on.

Following Parameters Through a Routine

Consider what happens when a caller invokes a routine. The caller supplies the inputs described by the specification. Those inputs travel into the routine as parameters. Inside the routine, the task is carried out, which is the part the caller cannot see. Finally, a value comes back to the caller as the result. The specification tells the caller where to put the inputs and what result to expect, and that is enough to use the routine.

Callersupplies the inputsInput parametersinformation handed overRoutine taskhidden from the callerReturned resultoutcome the specificationpromisespassesentersproducesreturns to caller
When a caller invokes a routine, where do the parameters go, what happens inside the routine, and what value returns to the caller?

Notice which steps the specification covers. The parameters and the returned result are both on the exposed side of the boundary, because the caller must know them. The routine task sits in the middle, hidden. This is why the description of a routine focuses on inputs and outcomes rather than on the steps taken between them.

Same Idea in Hardware and Scientific Libraries

The idea is not specific to software toolboxes. The source describes a toolbox of graphical routines, a hardware card, and a library of mathematical methods. Each presents a published interface and keeps its internals out of view. The principle stays the same across all of them, so a user can work without understanding the underlying computations.

Panel buttons andinput slotexposedRoutine name,inputs, resultexposedGears and circuitshiddenInternal computationstepshidden
How is the exposed-interface versus hidden-implementation split arranged for a hardware device and for a scientific routine library?

In the hardware column, the panel and the input slot are what you use, while the gears and circuits do the work out of sight. In the library column, the name, inputs, and result are the published side, while the computation steps stay internal. The two columns have the same shape. Only the kind of system differs.

Mistakes When Reading a Description

  • Common mistake

    Treating the hidden implementation as required reading

    What it looks like

    Deciding you cannot call a routine until you have studied how its steps are carried out.

    Why it fails

    The source states that users can work without understanding the underlying computations. The description supplies what you need to call the routine correctly.

    Do this instead

    Start from the specification: the name, the inputs it expects, and the outcome it promises.

  • Common mistake

    Confusing the parameters with the result

    What it looks like

    Reading a description that says a routine takes a list and returns a mean, then handing over the mean as the input.

    Why it fails

    Parameters are the information handed over to the routine, and the result is the outcome that comes back. They sit at opposite ends of the flow.

    Do this instead

    Ask which value goes in and which value comes out, using the parameter and task description.

  • Common mistake

    Assuming an unlisted detail is guaranteed

    What it looks like

    Relying on a routine to behave in a way the description never mentions.

    Why it fails

    The specification is the only thing a user is relying on. Anything outside it belongs to the hidden implementation, and it may change.

    Do this instead

    Build your use of the routine only on what the specification states.

  • Common mistake

    Believing abstraction only applies to software

Practice: Splitting a Description

EASY

A toolbox entry reads: the routine named sortRecords takes a list of records and a field name, and returns the same records in ascending order of that field. The description does not say which sorting method is used internally. Write two lists: one naming what this API exposes and one naming what it hides.

Hints
  • Exposed items are the name, the parameters, and the task and result the caller can expect.
  • Hidden items are anything the description leaves out, such as the internal method used to do the work.
  • Check whether the description mentions the sorting algorithm. If it does not, it belongs on the hidden list.

Key Takeaways from the Boundary

  1. 1
    An API is a specification of the routines a toolbox or device makes available, the parameters each one takes, and the task each one performs.
  2. 2
    Abstraction exposes only the information the outside world needs while hiding implementation details behind the interface.
  3. 3
    Callers work from the exposed side of the boundary: the name, the inputs, and the expected outcome.
  4. 4
    The same exposed-versus-hidden split applies to hardware devices and to scientific routine libraries.

At a glance

  • An API is a specification of the routines a toolbox or device makes available, the parameters each one takes, and the task each one performs.
  • Abstraction exposes only the information the outside world needs while hiding implementation details behind the interface.
  • To use a routine correctly, you need its name, the inputs it expects, and the outcome it promises, not its internal steps.
  • The same exposed-versus-hidden split applies to hardware devices and scientific routine libraries.