This site requires JavaScript, please enable it in your browser!
Greenfoot back
davemib123
davemib123 wrote ...

2015/11/8

using 'return' ?

davemib123 davemib123

2015/11/8

#
In an if-statement why is 'return' used? For example I have this, no return:
1
2
3
4
5
6
7
8
9
10
11
private void stopWalking()
    {
        if (facingRight)
        {
            setImage (RMidle);
        }
        else
        {
            setImage (LMidle);
        }
    }
Then I reviewed this from danpost's barriers scenario:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (getOneIntersectingObject(Barrier.class) != null)
       {
           if (dy == 0)
           {
               setLocation(getX(), getY() + 1);
               if (getOneIntersectingObject(Barrier.class) == null) return;
               setLocation(getX(), getY() - 2);
               if (getOneIntersectingObject(Barrier.class) == null) return;
               setLocation(getX() - dx, getY() + 1);
               return;
            }
           setLocation(getX() - dx, getY());
        }
davmac davmac

2015/11/8

#
You can think of "return" as meaning "stop executing any code in this method, and return to the calling method". The part of danpost's code you posted is not a complete method, so it's difficult to say, but there may be more code after the outer 'if' statement in the same method. The 'return' statement would prevent any such code from executing.
davemib123 davemib123

2015/11/8

#
thanks for the reply davmac. so with return stopping executing the code in the method, would I add:
1
2
3
4
5
6
7
8
9
10
11
12
13
private void stopWalking()
   {
       if (facingRight)
       {
           setImage (RMidle);
           return;
       }
       else
       {
           setImage (LMidle);
           return;
       }
   }
should i be adding a 'return' with each if-else statement? danpost code was this in the barriers scenario (http://www.greenfoot.org/scenarios/5182 ):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private void move()
    {
       int dx = 0, dy = 0;
       if (Greenfoot.isKeyDown("up")) dy--;
       if (Greenfoot.isKeyDown("down")) dy++;
       if (Greenfoot.isKeyDown("left")) dx--;
       if (Greenfoot.isKeyDown("right")) dx++;
       setLocation(getX() + dx, getY());
       if (getOneIntersectingObject(Barrier.class) != null)
       {
           if (dy == 0)
           {
               setLocation(getX(), getY() + 1);
               if (getOneIntersectingObject(Barrier.class) == null) return;
               setLocation(getX(), getY() - 2);
               if (getOneIntersectingObject(Barrier.class) == null) return;
               setLocation(getX() - dx, getY() + 1);
               return;
            }
           setLocation(getX() - dx, getY());
        }
        setLocation(getX(), getY() + dy);
        if (getOneIntersectingObject(Barrier.class) != null)
        {
            if (dx == 0)
            {
                setLocation(getX() + 1, getY());
                if (getOneIntersectingObject(Barrier.class) == null) return;
                setLocation(getX() - 2, getY());
                if (getOneIntersectingObject(Barrier.class) == null) return;
                setLocation(getX() + 1, getY() - dy);
                return;
            }
            setLocation(getX(), getY() - dy);
        }
    }
Super_Hippo Super_Hippo

2015/11/8

#
There is no code after the 'return' you just added, which could be executed if the 'return' wouldn't be there, so it doesn't matter if you add it there or not.
davmac davmac

2015/11/8

#
Right, you don't need to explicitly 'return' from the method unless you want to prevent the rest of the method from executing. In this case, the 'return's are pointless:
1
2
3
4
5
6
7
8
9
10
11
12
13
private void stopWalking()
   {
       if (facingRight)
       {
           setImage (RMidle);
           return;
       }
       else
       {
           setImage (LMidle);
           return;
       }
   }
In danpost's code, the 'return' on line 18 prevents any later lines from being executed. In the example above, there won't be any further lines executed anyway.
davemib123 davemib123

2015/11/8

#
what about these two, would I be including a return in any of these:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private void checkKeys() 
   
       isKeyPressed = false
       if (Greenfoot.isKeyDown("right") && Greenfoot.isKeyDown("left")) 
       
           isKeyPressed = true
           stopWalking(); 
       
       else if (Greenfoot.isKeyDown("right")) 
       
           isKeyPressed = true;
           facingRight = true;
           walkRight(); 
           shared.moveRight(this);
       
       else if (Greenfoot.isKeyDown("left")) 
       
           isKeyPressed = true;
           facingRight = false;
           walkLeft(); 
           shared.moveLeft(this);
       }
       if (!(isKeyPressed)) 
       
           stopWalking(); 
       
   
and
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void walkRight()
   {
       frame ++; 
       if(frame < 1 * animationDelay) 
       
           setImage(RMidle); 
       
       else if(frame < 2 * animationDelay) 
       
           setImage(RMwalk1); 
       
       else if(frame < 3 * animationDelay) 
       
           setImage(RMwalk2); 
       
       else if (frame < 4 * animationDelay) 
       
           setImage(RMwalk3); 
           frame = 0
       
   }
davmac davmac

2015/11/8

#
The fact that you're asking the question kind of implies you don't understand what I've already said. You need to use 'return' to stop any further code in the method being executed. Why would you need that in either of theses cases? You could use 'return' in either case. In the first case, this would remove the need for the 'isKeyPressed' variable.
danpost danpost

2015/11/8

#
I realize I have used a lot of 'return' statements within my 'move' method above; but, if you understood what the method was doing you would understand why I did so. The method first (lines 3 through 7) determines the direction the user is instructing the object to move in. Then, it checks horizontal movement (lines 8 through 21), followed by vertical movement (lines 22 through 35). Now, while checking for each direction of movment (horizontal and vertical), I wanted to allow movement in that direction if any movement in the other direction avoided intersecting a barrier -- thus causing the actor to glide along an edge of a slanted barrier. So, when a barrier is touched initially, adjustments are made to see if the barrier can be avoided. If at any time the barrier is avoided, no further action is required for movement and the method is forcibly exited. If a barrier cannot be avoided, one final statement needs to execute to place the actor back at its original location. This is undoubtedly not the most precise way to perform movement like this as horizontal movement has priority over vertical movement (which is not really wanted).
You need to login to post a reply.