0

I'm trying to understand the use of the return keyword at the end of a method.

I made a class to calculate a simple value:


public class Calc {
    
    Decimal monthlyWage     = 32;
    Decimal callCost        = 0.33;
    
    Public Decimal TotalValue(Decimal monthCalls){
        Decimal billValue = (callCost * monthCalls + monthlyWage);
        Return billValue;
    }
    
}

The class compiles, and I'm executing in anonymous windows like this:

Calc octoberCalc = new Calc();
octoberCalc.TotalValue(46);

I'm trying to use 'system.debug(TotalValue)', but it prompts me an error.

So my questions are:

1 - What does the 'return' keyword mean?

2 - And how can I obtain the total value of this bill?

Thanks in advance!!

1 Answer 1

2
  1. Same as in Java, you can see that Return signifies a return to the caller. If, in your method, you define a value to return (as you have done with public Decimal) you need to return a Decimal value back to the caller (where you called the method).
  2. You're returning a Decimal value from your method. Assign this to a variable and do with it what you'd like
Calc octoberCalc = new Calc();
Decimal billValue = octoberCalc.TotalValue(46);
//Debug the stored variable
System.debug(billValue);
//Debug the returned value from the method without assigning to variable
System.debug(octoberCalc.TotalValue(46));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .