#Why is return statement crucial in some areas and not in others

34 messages · Page 1 of 1 (latest)

stoic pelican
#
public class Interaction {
    public static void main(String[] args) {
        int n = 4;
        f(n);
    }

    public static int f(int n) {
        if (n <= 2) {
            n++;
            System.out.println(n);
            return n; // Add this return statement
        } else {
            int result = f(n - 1) * f(n - 2) + f(n - 2);
            System.out.println("f(" + n + ") = " + result);
            return result; // Add this return statement
        }
    }
}

here the code doesnt work without the return statement

ebon cipherBOT
#

This post has been reserved for your question.

Hey @stoic pelican! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

stoic pelican
#
public class Interaction {
  public static void main(String[] args) {

    for (int x = 0; x <= 10; x++){
      if (x % 2 == 0){
        System.out.println("even numbers" + x);
      }
      else{
        System.out.println("odd numbers" + x);
      }

    }





  }

}```
#

but here i dont need a return for the if and else

#

is it because i used it inside a for loop

#

in the second example

#

and in the first one i used if and else straight up

willow bramble
#

void -> no return
int (or anything other than void) -> needs a return

atomic cypress
#

why would you need to return result when you already printed it out?

shell ore
#

So it needs a return

atomic cypress
#

blud you dead

stoic pelican
atomic cypress
#

dont even talk

stoic pelican
#

thanks

ebon cipherBOT
# stoic pelican thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

atomic cypress
#

althought it is

#

if it was object void

#

you would need to return null

#
public Void onTest(final double input) {
        System.out.println(input);
        return null;
    }```
shell ore
atomic cypress
#

nah this men needs some beginner guide

shell ore
#

...

atomic cypress
#

w3 schools

#

this is too hard to understed

stark brook
#

ah the beginner trap w3schools

ebon cipherBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

junior ice
gloomy heath
#

...

formal fern
#

He's not wrong, note. But reporting would have been better practice

stark brook
#

heyyy yall remember the rainbow thread

yeah i feel that's what's gonna happen. yeah no.
@stoic pelican make a new thread if you still need help