SR1
SR1

Reputation: 253

Method parameters java

public double computePayment(double loanAmt,                   
                 double rate,
                 double futureValue,
                 int numPeriods) {
   double interest = rate / 100.0;
   double partial1 = Math.pow((1 + interest), 
                - numPeriods);
   double denominator = (1 - partial1) / interest;
   double answer = (-loanAmt / denominator)
                - ((futureValue * partial1) / denominator);
   return answer;
}

I am a beginner at Java and had a question about parameters. What exactly are they? I thought they were the variables used in the method, but now I see other variables like interest and partial1 being used in the method. These variables are derived from the parameter variables but still, what are parameters?

Thanks in advance.

Upvotes: 1

Views: 157

Answers (5)

Jet
Jet

Reputation: 3288

The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function definition, parameters appear in procedure definitions.

A parameter is an intrinsic property of the procedure, included in its definition. For example, in many languages, a procedure to add two supplied integers together and calculate the sum would need two parameters, one for each integer.

The function test has two parameters, named parm1 and parm2. It adds the values passed into the parameters, and returns the result to the subroutine's caller.

int test(int parm1, int parm2)
{
    return parm1 + parm2;
}

Upvotes: 0

Rahul
Rahul

Reputation: 77846

It's not certainly solely about Java but general programming language concept, here below mentioned variables are method/function argument which also makes a part of method signature whereas partial1 and interest are declared local variables which are declared within the function and so scoped within the function only.

             double loanAmt,
              double rate,
              double futureValue,
              int numPeriods

So, while calling the function computePayment you will have to provide those arguments without which it will not work/your program will not compile since those arguments are the dependency of the function.

Upvotes: 1

Niklas S.
Niklas S.

Reputation: 1054

Method Parameters are basically just a possibility to pass values (or references to objects) to methods. If you have a method which adds two values, you need those values before. They are passed through the Parameters for this method. So if your methods need some additional information to work with, you pass this information through parameters.

Upvotes: 1

Jan
Jan

Reputation: 2070

loanAmt, rate, futureValue and numPeriods are parameters while interest, partial1 etc. are local variables. Parameters are passed from the outside as part of the method call, local variables are for internal use.

Upvotes: 1

codeaholicguy
codeaholicguy

Reputation: 1691

The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.

You can look at Defining Methods for more details.

Upvotes: 1

Related Questions