Thread: break not working in a function within a switch

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    break not working in a function within a switch

    Hi. I am getting the 'break not working in a function within a switch or loop' error under someFunction(). My code looks something like this
    Code:
    do
    {
        switch(choice)
        {
            case 1: if(a==1)
                    {
                        someFunction(derp1, derp2, derp3);
                }
        }
    }
    Why does it say that the break; is not within a switch when someFunction is within a switch? How do I fix this?
    Code:
    void someFunction(int derp1, int derp2, int derp3)
    {
        if(derp1==1)
        {
            /*blah blah blah*/
            break;
        }
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    break doesn't propagate outside of functions.
    Devoted my life to programming...

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by edsoneicc View Post
    Why does it say that the break; is not within a switch when someFunction is within a switch?
    No. The nature of functions is that statements in them can't "see outside". The break inside someFunction simply does not know about how someFunction was called and has no way of knowing that someFunction was called from inside a switch.
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    Oh I see. Thanks. I think I should just call the function again to 'break out'.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by edsoneicc View Post
    Oh I see. Thanks. I think I should just call the function again to 'break out'.
    You could also use the return value of the function in the switch; currently it is void but you could use:

    Code:
        if(derp1==1)
        {
            /*blah blah blah*/
            return -1;
        } else return 0;
    In the switch:

    Code:
            case 1: if(a==1)
                    {
                        int x = someFunction(derp1, derp2, derp3);
                        if (x == -1) break;
                }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch won't break
    By GiantFurby in forum C Programming
    Replies: 20
    Last Post: 10-15-2008, 12:51 PM
  2. switch-case,Infinite while-loop ,break
    By babu198649 in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2008, 08:12 AM
  3. Question about break in switch statement
    By alone2002dj in forum C Programming
    Replies: 1
    Last Post: 12-09-2007, 08:42 PM
  4. break statement not within loop or switch
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-04-2006, 01:36 AM
  5. right use of switch(), case and break statements
    By Niara in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2006, 07:29 AM