Lesson 4.3: Return a Result
A function can return a value to the code that called it. A returned value can be printed, stored in a variable, compared in a condition, or used in another expression.
Returning and printing are different. Print sends information to the output panel. Return sends one result back to the caller. A function can return a value even when it prints nothing.
When a return command runs, that call ends. Commands later in the same function body do not run. The caller receives the returned result and continues.
Guided practice
Define a function named double with a parameter named number. Return number * 2. In the main program, call double with 6 and store its result in answer. Print answer.
Predict the parameter value, returned value, and stored value before running. Then call the same function with 9.
Java methods use a declared return type. For example, a Java method returning a whole number may begin with int doubleValue(...). The block version prepares you to follow that same flow of data.
Loading the block workspace...
Ready to move on?
Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.
Stuck on this lesson?
Ask about anything on this page. It can see which lesson you have open and which part you are reading.