--Introduction --Level that the poster understands --Try using it with code --Explain in the processing procedure -(Conclusion) What is the return value?
This article is just as unclear as I was a programming novice, as I was supposed to finally understand the return values that I couldn't understand at all after reading the description! This is an article that I will explain for those who say. Since I am not studying by reading books, the basic knowledge of the poster is low. (* Since you pointed out in the comments, I am making corrections) I would appreciate it if you could let me know in the comments about the points that are wrong in my recognition. Also, I don't understand the parable! Those who say that will apologize first. I'm sorry.
This is the range that I currently understand while learning Java. In other words, I don't know anything else, so I don't know if it's an applied expression. (Excuse)
--Basic type --Method meaning --Types of basic variables --Basic operator --Basic conditional branching --Repeat processing --Array
First of all, I will explain with the code. This time I will explain with this code.
qiita.jv
public class ReturnValue{
public static void main(String[] arg){
int a = 1;
int b = 5;
int c = a*b;
System.out.println(a + "x" + b + "=" + c);
}
}
The result of executing this code is
1x5=5
It will be. At this point this program can be run without any problems. Let's take a look at the program, assuming you want to introduce arguments and return values into this program and get the same result.
First, create a method different from the main method to set the arguments. Let's call it the back method here. And since we want to process the calculation formula (a * b) outside with an argument, specify the back method for the value of the variable c.
qiita.jv(2)
public class ReturnValue{
public static void main(String[] arg){
int a = 1;
int b = 5;
int c = back();
System.out.println(a + "x" + b + "=" + c);
}
static int back(){
}
}
Since the variable int is used, I created a back method with int type.
Execution result 1x5=5 In order to issue, it is necessary to redefine the variable in the back method and write the calculation formula.
qiita.jv(3)
public class ReturnValue{
public static void main(String[] arg){
int a = 1;
int b = 5;
int c = back();
System.out.println(a + "x" + b + "=" + c);
}
static int back(){
int a = 1;
int b = 5;
int c = a*b;
}
}
If you try to process it with the back method, it will be like this. However, since the same description is described in another method and the return value must be set for the int type, an error will occur in the first place. Describe return to return to return value to return processing result. In addition, I would like to inherit the values of variables a and b to the back method using arguments. Then
qiita.jv(4)
public class ReturnValue{
public static void main(String[] arg){
int a = 1;
int b = 5;
int c = back(a,b);
System.out.println(a + "x" + b + "=" + c);
}
static int back(int x,int y){
int a =x*y;
return(a);
}
}
It will be like this. First, describe the variables a and b that you want to inherit in the () of the return value "int c = back ();" that you want to return the processing result of the back method. In this case, there are two variables, so separate them with ",". Then, in the back method that is the inheritance destination, prepare a device that accepts the inherited values and variables a and b. This time, I prepared variables x and y. At this point, the values of variables a and b of the main method are inherited by variables x and y of the back method. In other words
int a = 1 = int x int b = 5 = int y
It means that. Redefine int a with the values of variables x and y that inherited the value of the main method. And finally, the value of the variable a, which is the processing result so far, is returned up to "int c". Since it is returned (a) to int c, the processing result of the processing result 1 * 5 in the back method is assigned to the variable c.
int c = 5
It will be.
System.out.println(a + "x" + b + "=" + c);
Is processed by the main method, so the value of the variable a redefined by the back method is not reflected. Therefore, by substituting the assigned value and character string as usual,
1x5=5
And you get the same result as an expression without arguments and return values.
Up to this point, I explained with the code, but I think that some people may not understand well when going out and back. I was confused when I heard such an explanation.
Therefore, I would like to organize and explain the series of steps here. I'm sorry many times, but please take a look at the final completed code.
qiita.jv(4)
public class ReturnValue{
public static void main(String[] arg){
int a = 1;
int b = 5;
//Step 1
int c = back(a,b);
//Step 2
//Step 6
System.out.println(a + "x" + b + "=" + c);
//Step 7
}
static int back(int x,int y){
//Step 3
int a =x*y;
//Step 4
return(a);
//Step 5
}
}
This time, the processing procedure is described in the comments so that you can see the code. (It's easy to understand!) Now, I would like to explain how the process is performed again according to the procedure.
Step 1 It defines variables a and b.
Step 2 The value to be inherited by the back method is defined in the variable c.
Step 3 From the main method, the values of variables a and b could be inherited by the vessels of the back method, variables x and y. This gives int x = 1 and int y = 5.
Step 4 Redefine the variable a. The result of the formula for the variables x and y inherited in step 3 is redefined in the variable a.
Step 5 By return, the variable a redefined by the back method that is the processing result is returned to the caller.
Step 6 The variable a redefined in the back method is assigned to the variable c in the main method. As a result, int c = 1 * 5 = 5.
Step 7 The value assigned to each and the character string are concatenated and output.
The above is the processing procedure of the explained code.
In other words, the return value represents the processing result. (And explained in the comments.) In this case, it's the calculation result. Since the return value reflects the result of the processing called calculation, the return value = the processing result.
I'm sorry that I didn't understand at all because I felt like I understood it, I couldn't understand the explanation, and I added extra elements.
Recommended Posts