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
public Decimal calculateDiscount(Decimal price, Decimal percentOff) {— this time the return type is a real type,Decimal, notvoid.Decimal discountAmount = price * (percentOff / 100);— computes the discount amount.return discountAmount;— hands the computed value straight back to the caller. Unlike thevoidmethods from earlier lessons, this one produces something usable.}— closes the method.- (blank line)
Decimal savings = calculateDiscount(200, 15);— the returned value is stored insavings.System.debug(savings);— logs30.
return exits immediately
public String classifyAge(Integer age) {
if (age < 18) {
return 'Minor';
}
return 'Adult';
}
public String classifyAge(Integer age) {— takes an age, returns a classification.if (age < 18) {— checks whether the age is under 18.return 'Minor';— the momentreturnruns, the method ends right there — nothing after it in that path executes.}— closes theif.return 'Adult';— only reached if line 2 was false — ifageis12, this line is never even reached, since line 3 already fired.}— 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
public Decimal calculateCommission(Decimal salePrice, Decimal commissionRate) {— takes the sale price and the commission rate as a percentage.return salePrice * (commissionRate / 100);— computes and returns the commission in one step, without a separate variable.}— closes the method.- (blank line)
Decimal commission = calculateCommission(1500000, 5);— calls it with a sale price and a 5% rate.System.debug(commission);— logs75000.
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;
Return Values Quiz
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.