We can pass data to java functions using parameters. Parameters are list of coma separated variables provided to a function in the parentheses. We can provide as many parameters as we need. In java programming language parameter name must follow data type of parameter.
Example
Following example shows a function with two parameters of type integers. The value in parameters is added and returned.
public class Main {
void add(int num1, int num2) {
int sum = num1 + num2 ;
System.out.println("Sum of numbers: " + sum);
}
}
Java function Arguments
Function parameters act variables in the body of function. Arguments are values or variables passed to function when function is called. Following example displays arguments of a function:
public class Main {
void add(int num1, int num2) {
int sum = num1 + num2 ;
System.out.println("Sum of numbers: " + sum);
}
public static void main(String[] args) {
Main obj = new Main();
obj.add(800,40);
}
}
800 and 40 are arguments in above example.