A collection of multiple processes and named as one process.
-Since it is described in functional units, the range of correction can be limited. ・ The visibility of the program will be improved and it will be easier to understand the whole. -Working efficiency is improved by combining the same processing into one method.
python
public static return type method name(argument){
Specific processing to be executed when the method is called
}
//The return type describes the data type of the value of the result of processing in the method. If the processing result is a numerical value, int,If it is a character string, it will be a String.
//Method call
Method name(argument); //The return value is not available.
You can pass a value from the caller when calling the method.
python
public static void main(String[] arg) {
hello("Yamamoto"); //helloメソッドを呼び出している。Yamamotoという文字列のデータを(Actual argument)Is passed to the hello method.
hello("Suzuki");
hello("Kitamura");
}
public static void hello(String name) { //When calling a method, Yamamoto assigns it to the Ring type variable name as a formal argument.
System.out.println(name + "Hi,"); //The data assigned to the variable name is output here.
}
//Execution result
Mr. Yamamoto, Hello
Suzuki, Hello
Kitamura, Hello//Becomes
//When there are multiple arguments
public static void main(String[] args) {
add(100,20); //Execution result x+ y = 120
add(200,50); //Execution result x+ y = 250
}
//Add method that accepts multiple values
public static void add(int x, int y) {
int ans = x + y;
System.out.println(x + "+" + y + "=" + ans);
}
The value (data) returned from the called method to the calling method is called the return value or return value.
python
return Return value; //This will allow you to return a value to the calling method.
//This return value is the execution result of the method.
Recommended Posts