The examples below use AP CSP Pseudocode. For Java, see Procedural abstraction.

Abstractions in computer science hide details to manage complexity. Procedural abstraction mean breaking a program into pieces, implementing each piece as a procedure (also known as a method or a function), and combining calls to the procedures to form the overall program.

Procedural abstraction makes programs easier to write because:

Commonly used abstractions include:

Example: Statistics from test scores

Consider the problem of accepting test scores from a user then calculating and displaying statistics about the scores. Statistics include the mean and the standard deviation.

Score (0-100): 50
Score (0-100 or -1 when done): 75
Score (0-100 or -1 when done): 100
Score (0-100 or -1 when done): -1
Mean: 75.0 , Standard deviation: 20.41

The above shows a sample run of the program. The values 50, 75, 100, and -1 (a sentinel) are input by the user. Everything else shown is displayed by the program.

See Input with a sentinel for more details (with code in Java).

getTestScores procedure

PROCEDURE getTestScores()
{
    DISPLAY("Score (0-100):")
    score ← INPUT()

    REPEAT UNTIL((score ≥ 0) AND (score ≤ 100))
    {
        DISPLAY("Invalid score, Score (0-100):")
        score ← INPUT()
    }

    scores ← []

    REPEAT UNTIL((score < 0) OR (score > 100))
    {
        APPEND(scores, score)

        DISPLAY("Score (0-100 or -1 when done):")
        score ← INPUT()
    }

    RETURN(scores)
}

Accepting all and only valid test scores from the user is more complicated than it may at first appear. Invalid scores must be rejected. At least 1 valid score must be accepted. The user must be able to indicate when they are done entering scores.

Each significant task in a program should be written as a procedure (often called a method or a function). The procedure should be testable before more of the program is written.

The getTestScores procedure handles all of the requirements and returns a list of valid test scores entered by the user. The method does not calculate or display statistics about the scores. Calculating statistics about the scores should be done in separate methods. Displaying the calculated statistics should be done in a separate method.

Testing getTestScores

DISPLAY(getTestScores())

The getTestScores procedure could be tested by calling the procedure and displaying the returned list.

If the user entered: 50, 75, 100, and -1
the test code would display: [50, 75, 100]

Note: The AP CSP Pseudocode is intended for questions on the AP CSP Exam. There are some unofficial websites that execute the pseudocode as if it was real code. The version of this page in Java (linked at the top) contains code that can actually be run.

getTestScores as an abstraction

Once the getTestScores procedure has been tested, and any errors fixed, it becomes an abstraction. Whenever we need to accept test scores from the user, we can call the getTestScores procedure without worrying about how the code inside works.

Many development environments support collapsing procedures so that only the procedure header is visible (and the code inside the procedure is hidden). This is a visual represntation of abstraction.

The procedure header is:

PROCEDURE getTestScores()

getMean procedure

PROCEDURE getMean(scores)
{
    sum ← 0

    FOR EACH score IN scores
    {
        sum ← sum + score
    }

    RETURN(sum / LENGTH(scores))
}

The getMean procedure accepts scores, a list of test scores, as a parameter. The procedure calculates and returns the mean of the scores.

The getMean procedure does not call the getTestScores procedure. Combining calls to the procedures to form the overall program is handled later.

Testing getMean

scores ← [50, 75, 100]
DISPLAY(getMean(scores))

The getMean procedure can be tested by

The test code would be expected to display 75.0.

The test code does not require user input. This allows the test to be quickly repeated if an error is detected and corrected.

Additional test cases should be used to determine that getMean works correctly; however, that is beyond the scope of this page.

getMean as an abstraction

Once the getMean procedure has been determined to work, it can be called without worrying about how the code inside works. This will be useful when calculating the standard deviation.

Library procedures pow and sqrt

Many programming languages provide procedures for common operations. These procedures are made available in a library for use by programmers.

For this demonstration, assume the availability of the procedures pow and sqrt that behave as described below.

pow(base, exponent) returns base raised to the exponent power.

For example, pow(2, 8) returns 256.

sqrt(num) returns the square root of num.

For example, sqrt(16) returns 4.

getStandardDeviation() procedure

PROCEDURE getStandardDeviation(scores)
{
    mean ← getMean(scores)
    sumOfSquares ← 0

    FOR EACH score IN scores
    {
        sumOfSquares ← sumOfSquare + pow(score - mean, 2)
    }

    RETURN(sqrt(sumOfSquares / LENGTH(scores)))
}

Calculating the standard deviation requires calculating the mean. The getMean procedure is called to calculate the mean. When calling getMean within getStandardDeviation, it is not necessary to remember how the code inside getMean works. This is abstraction (hiding details that are not relevant right now).

Testing getStandardDeviation()

scores ← [50, 75, 100]
DISPLAY(getStandardDeviation(scores))

The getStandardDeviation procedure could be tested using the same approach as the getMean test.

The test code would be expected to display 20.412.

Overall program

scores ← getTestScores()
mean ← getMean(scores)
stdDev ← getStandardDeviation(scores)

DISPLAY("Mean:")
DISPLAY(mean)
DISPLAY(", Standard deviation:")
DISPLAY(stdDev)

The overall program calls the procedures and handles the return values.

While writing the overall program, it is not necessary to worry about how the code inside each procedure works. All that is necessary is understanding how each procedure is called and what each procedures does (not how it does it).

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on Procedural abstraction in AP CSP Pseudocode