Free degree-level programming lessons for careful independent study.

Degree Level Programmes · Programming 1 · Lesson 6

Calling and Returning with Methods

See a method call as a temporary jump away from the current flow, followed by a return to the caller.

Lesson Overview

See a method call as a temporary jump away from the current flow, followed by a return to the caller.

Portfolio focus: Use one Java library method that returns a value.

ConceptFlow of control
Run fileCallingReturningMethodsDemo.java
Baseline12
Evidence3 tasks

Starter: think before typing

Before running this flow of control example, find the line where the main idea becomes active. Write a prediction: what must already be true for that line to work, and what should be different after it runs? The checked run ends with `12`; predict how the focus line helps produce that evidence.

Learning Objectives

  • Call an existing Java method.
  • Store a returned value.
  • Explain where control goes during a call.
  • Trace the value that comes back.

Learning Outcomes

  • By the end of the lesson, you can call an existing Java method.
  • By the end of the lesson, you can store a returned value.
  • By the end of the lesson, you can explain where control goes during a call.
  • By the end of the lesson, you can trace the value that comes back.

Why this idea exists

Method calls exist because programs need to reuse named behaviour without copying the same instructions everywhere. A call temporarily transfers control to another block of code and then returns to the caller.

This idea comes from subroutines and procedures in earlier programming languages, where programmers needed a disciplined way to split large tasks into smaller, reusable units.

In the programming arc, calling and returning prepares learners for decomposition, APIs, recursion and object behaviour. Once you can follow a call stack, a program stops being a single long script and becomes a set of cooperating parts.

Deep dive

Mechanism in this example

The important mechanism is visible around `int doubled = Math.multiplyExact(6, 2);`. Read it as a concrete move in the program, not as decorative syntax: identify what value, object, branch, call or boundary is being created at that point.

Design pressure

In the programming arc, calling and returning prepares learners for decomposition, APIs, recursion and object behaviour. Once you can follow a call stack, a program stops being a single long script and becomes a set of cooperating parts.

Failure mode to watch

For Calling and Returning with Methods, deliberately disturb the assumption behind `int doubled = Math.multiplyExact(6, 2);`: use an awkward value, missing input, wrong order of calls or boundary case. The useful question is how that disturbance exposes a weakness in the flow of control design.

Extension step

Extend the example by doing this: Draw the call and return path. The point is to make one small change that forces you to revisit the concept, rather than adding unrelated features.

Portfolio standard

The portfolio note should not repeat the lesson wording. It should show the edited code, the run result, and your own explanation of this evidence: explain what value was returned and where it was stored.

Lesson visual

A control-flow diagram showing main calling a helper and returning with an answer, like a question sent away and brought back.
A control-flow diagram showing main calling a helper and returning with an answer, like a question sent away and brought back.Download visual

Type this and run it

Create CallingReturningMethodsDemo.java, type the program, and run it before changing anything. This section is about reproducing the checked baseline.

public class CallingReturningMethodsDemo {
  public static void main(String[] args) {
    int doubled = Math.multiplyExact(6, 2);
    System.out.println(doubled);
  }
}

Build and run it with:

javac CallingReturningMethodsDemo.java && java CallingReturningMethodsDemo

Expected baseline: 12

Run the code in your browser

Use the editor as an experiment surface. First run the checked version, then make one small change to the part of the program that demonstrates flow of control and compare the new behaviour with the reference output.

Line-by-line explanation

Read the code as a sequence of responsibilities. The focus line for this lesson is int doubled = Math.multiplyExact(6, 2);; the surrounding lines prepare it, use its result or make the behaviour observable.

public class CallingReturningMethodsDemo {

This names the runnable class for the Calling and Returning with Methods example, giving the compiler and JVM one clear unit to build.

public static void main(String[] args) {

This is the program entry point. In Calling and Returning with Methods, it keeps the demonstration of flow of control in one traceable starting script.

int doubled = Math.multiplyExact(6, 2);

This introduces doubled as named state for Calling and Returning with Methods. Later lines can read, update, pass or print that specific value as evidence.

System.out.println(doubled);

This prints doubled as the observable evidence for Calling and Returning with Methods. The output lets the learner check whether the flow of control idea behaved as predicted.

}

This closes the innermost Calling and Returning with Methods block, so the immediately preceding method, branch or loop has finished.

}

This closes the outer Calling and Returning with Methods structure, returning the reader to the surrounding class or file.

Worked example

From code to explanation

Problem: Use Calling and Returning with Methods to complete a small portfolio-quality step: Use one Java library method that returns a value.

Method: Locate the line `int doubled = Math.multiplyExact(6, 2);`, explain the exact role it plays, then decide what you would change to extend the example without changing the whole program.

Reveal worked answer

The checked run should produce `12`. A strong answer links the result back to flow of control: what was created, selected, stored, called or protected, and why that matters for the portfolio task.

Trace the program

Before: Before the key operation, identify the relevant value, object, branch or resource that the flow of control concept depends on.

During: Trace `int doubled = Math.multiplyExact(6, 2);` as the Calling and Returning with Methods example executes. Say whether that operation creates data, checks a condition, calls behaviour, stores information or crosses a boundary.

After: Compare the run with the expected evidence: `12`.

Change: Now draw the call and return path, run again, and explain the smallest reason the behaviour changed.

Common misconception

A common mistake in calling and returning with methods is treating the example as a finished answer. For flow of control, the important question is narrower: which operation carries the idea, what does it make possible, and what would break if you changed it carelessly?

Quick checks

1. In this Calling and Returning with Methods example, what is the best reason to focus on `int doubled = Math.multiplyExact(6, 2);`?

2. Which evidence is strongest after you edit and rerun this example?

How to study this lesson

For Calling and Returning with Methods, predict how flow of control changes the run before you press Run.

Use the first portfolio task as your main edit: Use one Java library method that returns a value.

Use the second task as your variation: Draw the call and return path.

Finish with evidence, not a diary entry: Explain what value was returned and where it was stored.

Portfolio Practice

  1. Use one Java library method that returns a value.
  2. Draw the call and return path.
  3. Explain what value was returned and where it was stored.

Final self-check

Can you explain the key operation?

Explain the line identified in the quick check in one or two sentences. Your answer should say what it does before the output 12 appears.

Can you justify the portfolio evidence?

Your evidence should include the original run, one edited run, and a short note explaining how the edit affected flow of control.

Study route

Practise flow of control by predicting the Java example, typing it, running it in the browser, tracing the result and saving portfolio evidence.

Next, move into Packages, Imports and Names and carry forward one improvement from this lesson into the next program.