Discuss the concept of parameters. What are parameters for? What is the difference between formal parameters and actual parameters? Give an example in Java code that illustrates formal parameters and actual parameters.
Parameters are values that could be passed to methods. Methods process passed parameters in some ways. Parameters are used to modify methods executions results.
Inside method declaration and body formal parameters are used to represent passed parameters values. Values than were actually passed to method are called actual parameters.
Example - here "a" and "b" are formal parameters, x and y, containing values 6 and 7 respectively, are actual parameters:
public static int sum(int a, int b) {
return a + b;
}
public static void main(String[]) {
int x = 6;
int y = 7;
System.out.println(sum(x,y);
}
Comments
Leave a comment