Website for the College study materials and cheats you can use

Translate

Definition: Formal parameter, The actual parameter is much more straight forward

Definition:
1. Formal parameter:

The formal parameter appears at the definition of the method/subroutine. The formal parameter is a placeholder for the actual input of the method/subroutine when it's called. This is necessary since the compiler needs to know what kind of input is needed for the function when the function is called. Without the placeholder, the input format is unknown and the method or subroutine might not function as intended when called.

This can also be explained from the concept of the "black box". While we might not need to know the actual content of the box, we do need to be sure about what kind of input and output does the box needs/generate. Without knowing the precise format to communicate with the box, we won't be able to properly interact with the box and utilize the already developed function/method/subroutine.

2. Actual parameter:

The actual parameter is much more straight forward. It is the content that was passed to the called function. It might be different each time. However, it's format should be consistent with the formal parameter. The actual parameter provides the flexibility to the method/subroutine so that we don't need to develop separate methods for a similar purpose with slightly different details.

Example:
import javax.swing.JOptionPane;

public class Discussion_3 {
static void print(String message_formal) {
JOptionPane.showMessageDialog(null, message_formal);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String message_actual = "Hello, UoPEOPLE!";
print(message_actual);

}

}

In this example, the formal parameter is the message_formal in the method defining line. In the line, the message_formal tells Java that the print function requires a String input as a parameter. When the function is called by the main function, the function took message_actual as an actual parameter. This time, the actual parameter became the input that was given to the called "print" function. Accordingly, the message is displayed in the panel.

0 comments:

Post a Comment

Popular Posts

Categories

Contact Form

Name

Email *

Message *