Arguments and Parameters are closely related but refer to different concepts in method invocation and definition. Understanding the distinction is important for writing and reading method-based programs.
Argument
An argument is a value passed to a function at the time of its call, which is used by the function during execution. These values replace the variables defined in the function and allow it to work with actual data.
- Arguments are supplied when a function is invoked
- They provide input values to the function
- Passed arguments replace the parameters defined in the function
- The function executes using these passed values
Example:
public class Example {
public static int multiply(int a, int b) {
return a * b;
}
public static void main(String[] args) {
int x = 2;
int y = 5;
int product = multiply(x, y);
System.out.println("PRODUCT IS: " + product);
}
}
Output
PRODUCT IS: 10
Explanation: In the main() method, x and y are arguments passed to the multiply() method.
Parameter
A parameter is a variable defined in a function declaration that represents the data the function will receive. It acts as a placeholder for the values that are passed when the function is called.
- Parameters are defined during function declaration
- They act as variables inside the function
- They receive values from arguments at runtime
- Parameters and arguments may hold the same value but are conceptually different
Example:
public class Example {
public static int multiply(int a, int b) {
return a * b;
}
public static void main(String[] args) {
int x = 2;
int y = 5;
int product = multiply(x, y);
System.out.println("PRODUCT IS: " + product);
}
}
Output
PRODUCT IS: 10
Explanation: In the multiply() method, a and b are parameters that receive values from the arguments x and y.
Difference between an Argument and a Parameter
| Argument | Parameter |
|---|---|
| Values passed to a method during a method call | Variables defined in the method declaration |
| Used in the calling method | Used in the called method |
| Send data to the method | Receive data from arguments |
| Also called Actual Parameters | Also called Formal Parameters |
Exist at the time of method invocation | Exist during method execution |