M/F 2:30-3:20 |
CS 1130: Transition to OO Programming Spring 2016 |
Main
About: Overview Announcements Staff Consultants Calendar Materials: Texts DrJava Terminology Lectures: In-Class Web-Based VideoNote Assessment: Grading Assignments Labs Resources: CMS Piazza (link) Piazza (about) Java API Style Guide Academic Integrity |
Return StatementsIntroductionExecution of a return statement terminates execution of the method body and, hence, of the method call. The return statement in the body of a function differs from the return statement in a procedure or constructor. See below. The procedure and constructor bodiesWhen a procedure or constructor is called, the statements in its body are executed one at a time, in the order in which they appear. During this execution, execution of the statement:
terminates execution of the body, and thus of the procedure or constructor call —no further statement in the body is executed, once a return-statement is executed. A return-statement need not be included in the body of a procedure or constructor, but sometimes a body may be easier to write using a return-statement. The function bodyA function call yields a value. Therefore, execution of a function body must terminate by executing a return-statement of the form
where the type of the <expression> is the same as (or narrower than) the type of the result of the function. Execution of such a return-statement terminates the function body, and, thus, the function call, and yields the value of the<expression> as the result of the call. Since execution of a function body must terminate with execution of a return-statement, the last statement of a function body is usually a return-statement. However, return-statements may appear in other places as well. |