0
return type
why the hell is return type used in java what is the use of it and if it is used to display a value why can't we just use System.out.println () ; anyone please explain clearly... I'm confused.. note : I'm taking about the return used at the end of the program
2 ответов
+ 2
You don't need only all the time display a resulted calculated value.. You may need it for further manipulations in another function or in called function so in that case you must take into a storage by returning and use it. For ex :
you have function for returning a random primary number value as
int primeRand() {
...
return num;
}
in from main function you are calling this for to find which is existing in a list or not.. Like
int p =primeRand();
if(List.contains(p))
System.out.println ("exist") ; here contains is another function which returns a boolean value for p is in or not... If only is returned then it displays exist otherwise can't. Now see in all what happens if you displaying instead of returning value from function..?
Can you able to find same result as required..? Is you get it correct then?
+ 1
suppose you do an online shopping, don't you want your order to reach you? Or simply let the company use print statement there and let them use. :')
(same applies with programming).
A function is a block of codes which does a specific task. Mathematically, a function takes an input and spits out an output.
(exception: a function which does only printing or displaying information task)
https://www.sololearn.com/Discuss/2382658/?ref=app