Beginner 20 min read

Return Values

By the end of this lesson, you'll be able to:

  • Write a method that returns a value using return
  • Store a method's return value in a variable
  • Explain why return immediately exits the method

Prerequisites: "Parameters and Arguments"

Returning a value

public Decimal calculateDiscount(Decimal price, Decimal percentOff) {
    Decimal discountAmount = price * (percentOff / 100);
    return discountAmount;
}

Decimal savings = calculateDiscount(200, 15);
System.debug(savings); // 30
  1. public Decimal calculateDiscount(Decimal price, Decimal percentOff) { — this time the return type is a real type, Decimal, not void.
  2. Decimal discountAmount = price * (percentOff / 100); — computes the discount amount.
  3. return discountAmount; — hands the computed value straight back to the caller. Unlike the void methods from earlier lessons, this one produces something usable.
  4. } — closes the method.
  5. (blank line)
  6. Decimal savings = calculateDiscount(200, 15); — the returned value is stored in savings.
  7. System.debug(savings); — logs 30.

return exits immediately

public String classifyAge(Integer age) {
    if (age < 18) {
        return 'Minor';
    }
    return 'Adult';
}
  1. public String classifyAge(Integer age) { — takes an age, returns a classification.
  2. if (age < 18) { — checks whether the age is under 18.
  3. return 'Minor'; — the moment return runs, the method ends right there — nothing after it in that path executes.
  4. } — closes the if.
  5. return 'Adult'; — only reached if line 2 was false — if age is 12, this line is never even reached, since line 3 already fired.
  6. } — closes the method.

A real business example: Real Estate (Commission Calculator)

public Decimal calculateCommission(Decimal salePrice, Decimal commissionRate) {
    return salePrice * (commissionRate / 100);
}

Decimal commission = calculateCommission(1500000, 5);
System.debug(commission); // 75000
  1. public Decimal calculateCommission(Decimal salePrice, Decimal commissionRate) { — takes the sale price and the commission rate as a percentage.
  2. return salePrice * (commissionRate / 100); — computes and returns the commission in one step, without a separate variable.
  3. } — closes the method.
  4. (blank line)
  5. Decimal commission = calculateCommission(1500000, 5); — calls it with a sale price and a 5% rate.
  6. System.debug(commission); — logs 75000.

A real estate agent's commission calculation is exactly this shape: take inputs, compute one number, hand it back — clean, testable, and reusable for every sale.

Exercise

Coding challenge: write a method Decimal calculateTotal(Decimal price, Integer quantity) that returns price times quantity. Just define the method — it'll be called for you with different prices and quantities to check it works.

Show hint

return price * quantity;

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Return Values Quiz

1. What happens the moment a return statement executes?

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 1/1

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A method with a real return type hands a value back to whoever called it — that value can be stored, used in an expression, or passed straight into another method.