The examples below use AP CSP Pseudocode. For Java, see Methods with return values.

The content below assumes familiarity with Writing and calling procedures in AP CSP Pseudocode and Procedures with parameters in AP CSP Pseudocode.

A procedure in AP CSP Pseudocode can return 0 or 1 value to the code that called the procedure. A procedure with a return value can be called by other code and the return value used by the calling code.

findSmallest procedure with DISPLAY statement

PROCEDURE findSmallest(a, b, c)
{
    smallest ← a

    IF(b < smallest)
    {
        smallest ← b;
    }

    IF(c < smallest)
    {
        smallest ← c;
    }

    DISPLAY(smallest)
}

Procedure findSmallest finds and prints the smallest of its 3 numeric parameters a, b, and c.

See Finding the minimum or maximum for an in depth discussion of the algorithm.

This version of findSmallest displays the smallest value, which limits its usefulness.

findSmallest procedure with return value

PROCEDURE findSmallest(a, b, c)
{
    smallest ← a

    IF(b < smallest)
    {
        smallest ← b;
    }

    IF(c < smallest)
    {
        smallest ← c;
    }

    RETURN(smallest)
}

This version of findSmallest uses the same algorithm to find the smallest value; however, it returns the value instead of displaying it. Returning the smallest value makes this version of findSmallest more flexible.

A procedure that returns a value gives back the value to the code that called the method.

findLargest procedure

PROCEDURE findLargest(a, b, c)
{
    largest ← a

    IF(b > largest)
    {
        largest ← b;
    }

    IF(c > largest)
    {
        largest ← c;
    }

    RETURN(largest)
}

Procedure findLargest finds and returns the largest of its 3 parameters.

Calling a procedure with a return value

The value returned by a procedure call replaces the procedure call. A call to a procedure that returns a value can be used anywhere a value of that type could be used.

Example 1

DISPLAY(findSmallest(5, 8, 3))

The code segment displays 3.

The call to findSmallest passes 5, 8, and 3 as the values of a, b, and c, respectively. The call to findSmallest returns 3. The value 3 replaces the call to findSmallest. When the DISPLAY procedure is called, the call is:

DISPLAY(3)

Example 2

d ← 6
e ← 4
f ← 5;
biggest ← findLargest(d, e, f)
DISPLAY(biggest)

The code segment displays 6.

The call to findLargest passes 6, 4, and 5 as the values of a, b, and c, respectively. The call to findLargest returns 6. The value 6 replaces the call to findLargest. The assignment statement becomes:

...
biggest ← 6
...

Example 3

DISPLAY(findLargest(3, findSmallest(8, 7, 9), 6))

The code segment displays 7.

There are 3 procedure calls on this line: DISPLAY, findLargest, and findSmallest.

The call to findSmallest executes first and returns 7. The value returned by the call to findSmallest replaces the procedure call. The line is now:

DISPLAY(findLargest(3, 7, 6))

The call to findLargest executes next and returns 7. The value returned by the call to findLargest replaces the procedure call. The line is now:

DISPLAY(7)

The call to DISPLAY executes last and displays 7.

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on Procedures with return values in AP CSP Pseudocode