Defining the Method that just prints out a string, Why is there no return neccessary? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Defining the Method that just prints out a string, Why is there no return neccessary?

class MyClass { Static void sayHappyNewYear () { System.out.println ("Happy New Year World!"); } public Static void Main (String [ ] args) { sayHappyNewYear (); } } In the example where we add two Numbers there was a return value, right?. Is this different here in the Code above, because we use a method that already exists?

31st Dec 2016, 4:49 PM
eternallatesummer
1 Answer
+ 2
public class Program { static void addnumber(int x,int y){ System.out.println("sum is: " + (x+y)); } public static void main(String[] args) { addnumber(5,3); } } you see , i didnt use return here and it works...you are probablly asking why in my program i called function addnumber and send 2 numbers. in that same function output was called and we printed sum of 5 and 3 let se this program : public class Program { static int addnumber(int x,int y){ return (x+y); } public static void main(String[] args) { System.out.println("sum is" + addnumber(5,3)); } } in our main we first said :ok let's print and then called function where we send the numbers and execute sum 5+3 and then return that sum back to our main function
31st Dec 2016, 5:50 PM
Leonida17st
Leonida17st - avatar