in the previous task we were asked to help get the square of 5 and the answer was calling the square method in the main method
System.out.println(sqr(5));
so I tried the same logic here and tried to call the sum method the same way...I put
System.out.println(sum(2, 2));
why doesn't this work for me? it is in same format as in the previous example ....why is the answer
sum(2, 2);
and why is the previous question public static int
while this questions method is public static void
why cant I use println like in previous answer??
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
horst
14 January 2020, 07:32useful
I completely understand your confusion, but with the last two lines of your question you already spotted the reason for the different behavior of the two functions. The keywords "int" and "void" specify the return type of the function. (you can ignore "public" and "static" for now). The "int" function obviously returns an integer, whereas the "void" function returns nothing. I think the key to understand this is that there is a difference between "return" and "output":
Output is everything that is printed on the screen (or later: onto the user interface or when data/files are sent somewhere, etc.)
Return is what you internally get from the function when you call it (i.e. inside the program itself).
In your case of sum vs. square:
The "int square" returns the square of the input value, but it doesn't output it automatically. So the return value has to be processed further, e.g. by putting it into a print-statement or you can store it in another variable:
The "void sum" doesn't return anything, but it has its print-statement already right inside the function:
I will add a simple, but admittedly stupid example in a moment... +1
horst
14 January 2020, 07:46solution
Simple example:
You have a raw egg and want to eat a boiled one. (Given you own a pot, an oven, some water and a spoon to get the egg out of the hot water). Now you put the egg into the pot with water and turn on the heat.
Think of the next steps as two functions:
Function 1, the "boil egg" function: You add heat to the egg and wait for a few minutes.
Function 2, the "takeEggOutOfTheWater"-function: You take a spoon and lift the egg out of the water.
When the "boil egg"-function is called, the hot water will boil the egg and it RETURNS a boiled egg (but nothing else. It doesn't output the boiled egg, it just heats it up until its boiled).
The "void takeEgg..."-function just OUTPUTS the egg out of the pot without processing or modifying it any further.
I sincerely hope that this doesn't leave you more confused than before, I couldn't think of a more concise explanation right away. Don't hesitate to ask and I will do my best to clarify. Happy Coding!
+1
Jo88
14 January 2020, 23:44
ahh I still don't understand that well..
in the square task..I wrote
System.out.println(sqr(5));
SO for the next task ( the sum 2+2 task)
its the SAME problem as the taks asking me for the square..so wouldn't it be correct thinking for the answer to be
System.out.print(sum(2, 2));
WHY is it simply
sum(2, 2);
???
0
horst
15 January 2020, 00:14
Don't worry, you will get the idea soon enough. In the main method - which will be the starting point of nearly all of your programs - you can "call" other methods to perform certain actions (i.e. execute the code that you write in those methods). Just carefully compare the square method with the sum method:
The above method takes a number as an input (the variable a is the placeholder for that number). Then it multiplies it by itself and returns the value. Now look at the given sum method in your current task:
You see that in line three this method already has a print-statement written into it, which will automatically be executed as soon as you call the method. This is why you don't have to print the result manually. (If you were to delete this line, the sum-method wouldn't print anything by itself. ) 0