What does return mean here ?!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nahla Nagy
    New Member
    • Jan 2011
    • 6

    What does return mean here ?!

    if(current==NUL L)
    {
    cout<<"Can't find the element to be deleted " <<endl;
    return;
    }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It means exit the function.

    Comment

    • Nahla Nagy
      New Member
      • Jan 2011
      • 6

      #3
      and what will be the difference if i put "break;" ?!

      Comment

      • Nahla Nagy
        New Member
        • Jan 2011
        • 6

        #4
        and and this snippet it's kinda useless,isn't it ?!
        it does the job when there are other statements,yeah ?!

        Comment

        • Nahla Nagy
          New Member
          • Jan 2011
          • 6

          #5
          and what will be the difference if i put "break;" ?!

          and in this snippet it's kinda useless,isn't it ?!
          it does the job when there are other statements,yeah ?!

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Break exits the innermost flow control structure. In this case, break exits the if statement. Yes, break would be pointless in that statement. Return would not depending on what you're trying to do. I have no idea what you mean by that last line.

            Comment

            • Nahla Nagy
              New Member
              • Jan 2011
              • 6

              #7
              Haha,never mind,it was a stupid question.
              Let me be sure that i got it,,return exit the function,but break get me out of the conditions and jumps to the code after it ,right ? :$

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                That's right. But in the case of breaks, it only breaks out of one control flow. If you had a loop in a loop, it only breaks out of the inner most loop.

                Comment

                • Nahla Nagy
                  New Member
                  • Jan 2011
                  • 6

                  #9
                  Thanks a lot ! =)

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    break will exit from the innermost switch, for, do, or while block. Think of it as a goto the first statement after the block. It has nothing to do with if statements. It is an error to use break if not within a switch, for, do, or while block.

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #11
                      Sorry, my mistake. It's been a while since I've used C. Don's right. I guess there really isn't a need to break out of an if statement.

                      Comment

                      Working...