Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Difference between goto and function calls

Difference between goto and function calls

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    Hi all, I'll try to explain my question as much as quickly. Actually its not a question, but a clarification I am looking for. Say I have a two code blocks which is doing two different things. I can implement those two blocks in two functions and call as I need from a third function. Or else, I can implement those two blocks in the third function and used goto jump between blocks. So what is the difference between two? Thanks for all the comments in advance.

    If you've never failed... You've never lived...

    E L 3 Replies Last reply
    0
    • C CodingLover

      Hi all, I'll try to explain my question as much as quickly. Actually its not a question, but a clarification I am looking for. Say I have a two code blocks which is doing two different things. I can implement those two blocks in two functions and call as I need from a third function. Or else, I can implement those two blocks in the third function and used goto jump between blocks. So what is the difference between two? Thanks for all the comments in advance.

      If you've never failed... You've never lived...

      E Offline
      E Offline
      Eugen Podsypalnikov
      wrote on last edited by
      #2

      // So what is the difference between two? I would say - stack - accountability for the heap (de-)allocations - perfomance - re-usage of the functions - readability What thesis does stay misty for you ? :)

      They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)

      1 Reply Last reply
      0
      • C CodingLover

        Hi all, I'll try to explain my question as much as quickly. Actually its not a question, but a clarification I am looking for. Say I have a two code blocks which is doing two different things. I can implement those two blocks in two functions and call as I need from a third function. Or else, I can implement those two blocks in the third function and used goto jump between blocks. So what is the difference between two? Thanks for all the comments in advance.

        If you've never failed... You've never lived...

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        A goto wont add stuff to the stack like a function call will. But the problem is that the code that you goto has to get back somehow, which means embedding a label in your code, which means you goto code would be better just put in your calling code. So really, you should use a func, it will be easier. Gotos are useful for exiting funcs in error conditions. (Often replaced by a do{}while(0) these days).

        ============================== Nothing to say.

        1 Reply Last reply
        0
        • C CodingLover

          Hi all, I'll try to explain my question as much as quickly. Actually its not a question, but a clarification I am looking for. Say I have a two code blocks which is doing two different things. I can implement those two blocks in two functions and call as I need from a third function. Or else, I can implement those two blocks in the third function and used goto jump between blocks. So what is the difference between two? Thanks for all the comments in advance.

          If you've never failed... You've never lived...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          People will hate you if you use goto, it makes the code more difficult to test, debug and maintain; stick with functions.

          speaking as ...

          L 1 Reply Last reply
          0
          • L Lost User

            People will hate you if you use goto, it makes the code more difficult to test, debug and maintain; stick with functions.

            speaking as ...

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            This is pure dogma. Any code can be crap and hard to maintain and goto when used judiciously is perfectly OK. For example:

            myfunc()
            {
            do{
            if(!somefunc())
            continue;
            if(!someotherfunc())
            continue;

            }while(0);
            }

            Is heavy when compared to:

            myfunc()
            {
            if(!somefunc())
            goto end;
            if(!someotherfunc())
            goto end;

            end:
            }

            ============================== Nothing to say.

            L 1 Reply Last reply
            0
            • L Lost User

              This is pure dogma. Any code can be crap and hard to maintain and goto when used judiciously is perfectly OK. For example:

              myfunc()
              {
              do{
              if(!somefunc())
              continue;
              if(!someotherfunc())
              continue;

              }while(0);
              }

              Is heavy when compared to:

              myfunc()
              {
              if(!somefunc())
              goto end;
              if(!someotherfunc())
              goto end;

              end:
              }

              ============================== Nothing to say.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Erudite_Eric wrote:

              This is pure dogma.

              Not at all. And what's wrong with:

              myfunc()
              {
              if(somefunc() &&
              someotherfunc())
              {
              // do stuff
              }
              }

              speaking as ...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups