In this part public static int min(int a, int b, int c, int d) {
i copied from someone else in another question to solve it return min(min (a, b), min (c,d));
obviously the min (a,b) part i made was the classical previous examples we already did
int m2;
if (a<b)
m2=a
else
m2=b
return m2
So.....my question is.......why this min(c,d) part works?
I cant really understand why this worked....can you explain why?
Resolved
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
5 February 2019, 20:24
The min(a,b,c,d) method that returns min(min(a,b),min(c,d)) works because it is returning the result of that. take this step by step:
1) first inner min(a,b) is an int that equals the minimum of a and b
2) second inner min(c,d) is an int that equals the minimum of c and d
3) the outer min * min(min(a,b), min(c,d)) * is the result of the minimum ints from the above two points.
you could even make it do 8 variables like this:
0
Carlos
5 February 2019, 21:22
Thats the part i dont get that much....you can call a fraction of the variables in a method?
I understand the part wich i call min(a,b) because is the whole method but calling a fraction of a method like min(c,d) can be done? why?
0
Guadalupe Gagnon
5 February 2019, 21:30useful
because min(a,b) is the same as an int. Wherever you see min(a,b) that is not an equation, that is a solid int that is the result of the minimum of the 2 numbers.
Lets walk through some code, lets say a=1, b=2, c=3, d=4:
So min(1,2) is the same as just 1 and min(3,4) is the same as just 3:
min(min(1,2),min(3,4)) is the same exact thing as typing min(1,3).
+2
Guadalupe Gagnon
5 February 2019, 21:34useful
And you are not calling a fraction, you are calling the whole min(int a, int b) method inside the min(int a, int b, int c, int d) method. The computer knows which one to use by the inputs. If your inputs are 1 and 2, then min(a,b) will be called. If your inputs were 1,2,3,4 then the min(a,b,c,d) method would be called. If your inputs were 1,2,3 then the program would error out and not run because there is no min method that excepts three numbers (unless you write one).
+2
Guadalupe Gagnon
5 February 2019, 21:36useful
CodeGym will explain the concept of method overloading soon if it has not already. You can read about it here if interested:
https://beginnersbook.com/2013/05/method-overloading/
+1
Carlos
5 February 2019, 21:40
They havent explained overloading yet, maybe that is why i am a little confused, but anyway...thats how this course is designed.... is designed to make people help each other.
Another question has arised.... how is the method min (a,b,c,d) knowing what to do if i only put the parameters in the method min (a,b) ....how it does know what to do with the min(c,d) if i havent included instructions in the min (a,b,c,d) ?
0
Guadalupe Gagnon
5 February 2019, 21:52solution
Because c and d are just variables for ints, you can name them anything that you want, but they are still ints.
When you type the code min(c,d) the method min(int a, int b) is called.
These are all good questions and I remember when I was first learning this. It was a little complicated to wrap my head around. I would recommend trying out different things in your code. Rename variables so that you can see that the result still does not change. Try calling the code different ways, such as this in main:
↑ can you guess what is going to happen? +2
Carlos
6 February 2019, 00:06
now i understood and you solved all my doubts regarding this issue! Guadalupe, you Rock !
PS: the result is 1
0