AP® Computer Science A glossary

Recursion

Also called: recursive method

Course: AP® Computer Science A Shows up in: Unit 4: Data Collections

A form of repetition in which a method calls itself; it needs at least one base case and at least one recursive call.

Appears in the course and exam description (CED): 4.16.A.14.16.A.24.16.A.34.17.A.1

A recursive method calls itself with parameter values that move toward a base case. Each call has its own parameters and local variables. Any recursive solution can also be written with a loop. On the AP® exam you trace recursive methods; writing them is not required.

Example

public static int sumDigits(int n)
{
    if (n < 10)
    {
        return n;                        // base case
    }
    return n % 10 + sumDigits(n / 10);   // recursive call
}
Learn it properly, with a checkpoint 4.16 Recursion

Related terms

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure