AP® Computer Science A glossary

Return statement

Also called: return

Course: AP® Computer Science A Shows up in: Unit 3: Class Creation

A statement that sends the flow of control back to the caller, and in a non-void method sends back a value compatible with the return type.

Appears in the course and exam description (CED): 3.5.A.33.5.A.4

In a non-void method the return expression is evaluated and its value is returned (return by value). A return inside a loop or an if exits the whole method, and a void method may use return; to leave early.

public class Finder
{
    public static int firstEven(int a, int b)
    {
        if (a % 2 == 0)
        {
            return a;    // the method ends here
        }
        return b;
    }

    public static void main(String[] args)
    {
        System.out.println(firstEven(4, 7) + " " + firstEven(3, 8));
    }
}

Output:

4 8
Learn it properly, with a checkpoint 3.5 Methods: How to Write Them

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