11 Replies - 2035 Views - Last Post: 11 August 2012 - 01:38 PM Rate Topic: -----

#1 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Understanding 'Return' values in methods - Java

Posted 10 August 2012 - 09:35 PM

I'm trying to understand the purpose of putting return values in methods. In this example, which I took from YouTube, the chap refers to this as "junk value". Can anyone, very simply, explain this to me? Does it have something to do with memory (if so, I understand it at a very basic level)?

public static int showPi (int a, int B)/>{
double smallPi = 3.140;
System.out.println(myPi);
return 1;
}


Edit: He changes this method later on to an addition method, hence the parameters, so please ignore that.

This post has been edited by TechSyndrome: 10 August 2012 - 09:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Understanding 'Return' values in methods - Java

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7884
  • View blog
  • Posts: 13,400
  • Joined: 19-March 11

Re: Understanding 'Return' values in methods - Java

Posted 10 August 2012 - 10:08 PM

Take down the name of this fellow, and any time you're getting advice from YouTube, if it comes from him, ignore it.

There is no reason at all for this method to return an int, there is no reason for it to take any parameters, and there is no reason to have a method to display a grossly truncated and in fact incorrect value for pi when the constant can be had in a much more accurate form from the Math class.

In short, everything about this method is wrong, and it would be futile to even start cataloging the ways in which it should be repaired. Much better to simply ignore it, forget that you ever saw it, and go learn to write useful functions to do useful things.


So, when do you return a value from a function? Any time the purpose of the function is to calculate a value. Suppose you have a function that calculates (by some means) the value of pi. Having calculated it, do you simply show it once and let it go away? Of course not: you return it as a value so that the calling function can make use of it or store it, or display it if they want to.

On the other hand, if a function exists to perform some side effect it should usually not return a value. If, for example, you have a function that draws a circle in your GUI, it would be foolish to have it also return some value.

With functions, you should generally show or tell, but not both.
Was This Post Helpful? 2
  • +
  • -

#3 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 234
  • Joined: 29-February 12

Re: Understanding 'Return' values in methods - Java

Posted 10 August 2012 - 10:10 PM

generally an int return method is used when a int changes based on computations preformed during the method, then returning the statement for use, so basically it is used for regularly changing numbers and such.

as for the method above, that just simply makes no since... if you wanted to make a double and print it out, you should not use a int return method..
Was This Post Helpful? 1
  • +
  • -

#4 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: Understanding 'Return' values in methods - Java

Posted 10 August 2012 - 10:14 PM

I deeply apologise - I murdered the code, and also, the way I communicated it was out of context. It'll be better if you watch the video (if you wish to), rather than me explaining my mistake further.


My link

Ok, now back to the meat of my question. I'll reply individually to your comments. I appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#5 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: Understanding 'Return' values in methods - Java

Posted 10 August 2012 - 10:19 PM

View Postjon.kiparsky, on 10 August 2012 - 10:08 PM, said:

Take down the name of this fellow, and any time you're getting advice from YouTube, if it comes from him, ignore it.

There is no reason at all for this method to return an int, there is no reason for it to take any parameters, and there is no reason to have a method to display a grossly truncated and in fact incorrect value for pi when the constant can be had in a much more accurate form from the Math class.

In short, everything about this method is wrong, and it would be futile to even start cataloging the ways in which it should be repaired. Much better to simply ignore it, forget that you ever saw it, and go learn to write useful functions to do useful things.


So, when do you return a value from a function? Any time the purpose of the function is to calculate a value. Suppose you have a function that calculates (by some means) the value of pi. Having calculated it, do you simply show it once and let it go away? Of course not: you return it as a value so that the calling function can make use of it or store it, or display it if they want to.

On the other hand, if a function exists to perform some side effect it should usually not return a value. If, for example, you have a function that draws a circle in your GUI, it would be foolish to have it also return some value.

With functions, you should generally show or tell, but not both.


Thank you. So, for example, we have this addition method:

public static int addThem (int a, int B)/>{
int c = a + b;
return c;



So, the result of this calculation is stored in the variable c and then I code return c. When you say "do you simply show it once and let it go away?", would the whole method disappear at runtime or something if I didn't put a return value? I apologise in advance if I've confused you with my question. There's likely to be some misunderstanding on my part which I hope to rectify as I'm learning.

This post has been edited by TechSyndrome: 10 August 2012 - 10:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7884
  • View blog
  • Posts: 13,400
  • Joined: 19-March 11

Re: Understanding 'Return' values in methods - Java

Posted 10 August 2012 - 10:46 PM

If you display a value to the console in a function, you limit yourself in two ways. The first is that this value is not available for other sorts of display. For example if you wanted to show it in a text field in a GUI, or send it to some display widget in Android, you'd be out of luck: all your method can do is to print it to the console. The other limitation is that the value is only available to you once, and must be calculated each time you call the function. This means, among other things, that it's not available for comparison to other values, it can't be used for sorting, or what have you.

Quote

When you say "do you simply show it once and let it go away?", would the whole method disappear at runtime or something if I didn't put a return value?


Yes. In your example, c is declared in the method, and therefore it can only be read inside that method. After that call returns, c is gone as if it never existed.
On the other hand, if you return the value of c, you have the option of using it or storing it for future use. In that case, it can persist beyond the life of the method.

This post has been edited by jon.kiparsky: 10 August 2012 - 10:47 PM

Was This Post Helpful? 1
  • +
  • -

#7 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: Understanding 'Return' values in methods - Java

Posted 11 August 2012 - 09:02 AM

View Postjon.kiparsky, on 10 August 2012 - 10:46 PM, said:

If you display a value to the console in a function, you limit yourself in two ways. The first is that this value is not available for other sorts of display. For example if you wanted to show it in a text field in a GUI, or send it to some display widget in Android, you'd be out of luck: all your method can do is to print it to the console. The other limitation is that the value is only available to you once, and must be calculated each time you call the function. This means, among other things, that it's not available for comparison to other values, it can't be used for sorting, or what have you.

Quote

When you say "do you simply show it once and let it go away?", would the whole method disappear at runtime or something if I didn't put a return value?


Yes. In your example, c is declared in the method, and therefore it can only be read inside that method. After that call returns, c is gone as if it never existed.
On the other hand, if you return the value of c, you have the option of using it or storing it for future use. In that case, it can persist beyond the life of the method.


Thank you. Just to confirm I have understood this, do you have any example of short and simple code, where this question is relevant? So, for example, code where the method returns a value, and code where there is not a return value. The reason why I ask is because I would like to see a real world example. If you don't have any code, or somewhere you can point me towards, that's cool. You've helped me more than enough.
Was This Post Helpful? 0
  • +
  • -

#8 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: Understanding 'Return' values in methods - Java

Posted 11 August 2012 - 09:09 AM

Also, I just had a look of a guy showing an example where there is a try/catch:

public static void divideByZero(int a ){
try
{

System.out.println(a/0);

}

catch (ArithmeticException e)
}

System.out.println("You can't do that");

System.out.println(e.getMessage());

e.printStackTrace();
}


Here there is not a return value, but isn't there a calculation in the try statement i.e. System.out.println(a/0);?

This post has been edited by TechSyndrome: 11 August 2012 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,272
  • Joined: 21-June 11

Re: Understanding 'Return' values in methods - Java

Posted 11 August 2012 - 09:15 AM

Here's an example:

// Calculates the square of a number and returns
int square(n) {
    int square = n*n;
    return square;
}

// Calculates the square of a number and prints it
void printSquare(n) {
    int square = n*n;
    System.out.println(square);
}

// ...
// somehwehre in another method

int fourSquared = square(4);
System.out.println("4 is " + fourSquared);
int fourSquaredSquared = square(fourSquared);
System.out.println("(4) is " + fourSquaredSquared);

// and now let's try this with the version that doesn't return
// int fourSquared = printSquared(4); // not allowed
System.out.print("4 is ");
printSquared(4);
// int fourSquared = squared; // not allowed

System.out.print("(4) is ");
// What now?
// I don't have a value for fourSquared so I can't do further calculations with it



View PostTechSyndrome, on 11 August 2012 - 06:09 PM, said:

Here there is not a return value, but doesn't isn't there a calculation in the try statement i.e. System.out.println(a/0);?


Yes, there is a calculation, but the result of the calculation is not returned, only printed.
Was This Post Helpful? 0
  • +
  • -

#10 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: Understanding 'Return' values in methods - Java

Posted 11 August 2012 - 09:22 AM

View Postsepp2k, on 11 August 2012 - 09:15 AM, said:

Here's an example:

// Calculates the square of a number and returns
int square(n) {
    int square = n*n;
    return square;
}

// Calculates the square of a number and prints it
void printSquare(n) {
    int square = n*n;
    System.out.println(square);
}

// ...
// somehwehre in another method

int fourSquared = square(4);
System.out.println("4 is " + fourSquared);
int fourSquaredSquared = square(fourSquared);
System.out.println("(4) is " + fourSquaredSquared);

// and now let's try this with the version that doesn't return
// int fourSquared = printSquared(4); // not allowed
System.out.print("4 is ");
printSquared(4);
// int fourSquared = squared; // not allowed

System.out.print("(4) is ");
// What now?
// I don't have a value for fourSquared so I can't do further calculations with it



View PostTechSyndrome, on 11 August 2012 - 06:09 PM, said:

Here there is not a return value, but doesn't isn't there a calculation in the try statement i.e. System.out.println(a/0);?


Yes, there is a calculation, but the result of the calculation is not returned, only printed.


Thank you. I'm looking though your code now.
Was This Post Helpful? 0
  • +
  • -

#11 TechSyndrome  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 135
  • Joined: 06-May 12

Re: Understanding 'Return' values in methods - Java

Posted 11 August 2012 - 10:24 AM

View Postsepp2k, on 11 August 2012 - 09:15 AM, said:

Here's an example:

// Calculates the square of a number and returns
int square(n) {
    int square = n*n;
    return square;
}

// Calculates the square of a number and prints it
void printSquare(n) {
    int square = n*n;
    System.out.println(square);
}

// ...
// somehwehre in another method

int fourSquared = square(4);
System.out.println("4 is " + fourSquared);
int fourSquaredSquared = square(fourSquared);
System.out.println("(4) is " + fourSquaredSquared);

// and now let's try this with the version that doesn't return
// int fourSquared = printSquared(4); // not allowed
System.out.print("4 is ");
printSquared(4);
// int fourSquared = squared; // not allowed

System.out.print("(4) is ");
// What now?
// I don't have a value for fourSquared so I can't do further calculations with it



View PostTechSyndrome, on 11 August 2012 - 06:09 PM, said:

Here there is not a return value, but doesn't isn't there a calculation in the try statement i.e. System.out.println(a/0);?


Yes, there is a calculation, but the result of the calculation is not returned, only printed.



Ok, I'd like to go through this step-by-step. First two methods:

// Calculates the square of a number and returns
int square(n) {
    int square = n*n;
    return square;
}

// Calculates the square of a number and prints it
void printSquare(n) {
    int square = n*n;
    System.out.println(square);
}



1st method: So you've created a method that accepts parameter integer n. When the user passes in an integer, the method then calculates and stores result in square. Then it returns square (I still don't know what this means, but I'm sure I'll get it).

2nd method: The return type is void, but doesn't the method return a value? Because the user still has to pass in an integer, and there is still a calculation?

I noticed that one of the differences between these two methods is that the 1st method doesn't print the result immediately, whereas the 2nd method does print the result immediately.

Ok, am I doing ok so far?
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,272
  • Joined: 21-June 11

Re: Understanding 'Return' values in methods - Java

Posted 11 August 2012 - 01:38 PM

View PostTechSyndrome, on 11 August 2012 - 07:24 PM, said:

1st method: So you've created a method that accepts parameter integer n. When the user passes in an integer, the method then calculates and stores result in square. Then it returns square (I still don't know what this means, but I'm sure I'll get it).


Returning a value means that that value will be the result of calling the method. I.e. if you do y = myMethod(arg1, arg2); then y will take the value returned by myMethod. If myMethod does not return a value (i.e. it is void), you're not allowed to write something like y = myMethod(arg1, arg2); - only myMethod(arg1, arg2);.

Quote

2nd method: The return type is void, but doesn't the method return a value?


No, the method does not contain the return keyword followed by a value, so it doesn't return a value.

Quote

Because the user still has to pass in an integer, and there is still a calculation?


Yes, those things are true, but that doesn't mean that the method returns a value.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1