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. Old code

Old code

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 5 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.
  • R Offline
    R Offline
    RomTibi
    wrote on last edited by
    #1

    Reviewing some old projects I met a function like this:

    bool func()
    {
    bool var = false;

    if(...)
        return var = false;
    else
        return var = true;
    

    }

    Actually what the function returns? I think true, because assigning can't be false.

    VII. 36. When you surround an army, leave an outlet free. Do not press a desperate foe too hard. SUN-TZU - Art of War

    P S D L 4 Replies Last reply
    0
    • R RomTibi

      Reviewing some old projects I met a function like this:

      bool func()
      {
      bool var = false;

      if(...)
          return var = false;
      else
          return var = true;
      

      }

      Actually what the function returns? I think true, because assigning can't be false.

      VII. 36. When you surround an army, leave an outlet free. Do not press a desperate foe too hard. SUN-TZU - Art of War

      P Offline
      P Offline
      Patrice T
      wrote on last edited by
      #2

      RomTibi wrote:

      Actually what the function returns? I think true, because assigning can't be false.

      There is an easy way to know ! - make a program that will execute that code.

      Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

      1 Reply Last reply
      0
      • R RomTibi

        Reviewing some old projects I met a function like this:

        bool func()
        {
        bool var = false;

        if(...)
            return var = false;
        else
            return var = true;
        

        }

        Actually what the function returns? I think true, because assigning can't be false.

        VII. 36. When you surround an army, leave an outlet free. Do not press a desperate foe too hard. SUN-TZU - Art of War

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #3

        Any assignment is also an expression which has the value of the assigned value.

        if(...)
            return var = false;  // returns false
        else
            return var = true;  // returns true
        

        Some programmers make use of this fact like this:

        int x;
        while ((x = SomeFunction()) > 0)
        {
        // use x for something..
        }

        Whether or not this is good style is up to personal preference..

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        1 Reply Last reply
        0
        • R RomTibi

          Reviewing some old projects I met a function like this:

          bool func()
          {
          bool var = false;

          if(...)
              return var = false;
          else
              return var = true;
          

          }

          Actually what the function returns? I think true, because assigning can't be false.

          VII. 36. When you surround an army, leave an outlet free. Do not press a desperate foe too hard. SUN-TZU - Art of War

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          RomTibi wrote:

          I think true, because assigning can't be false.

          True (regardless).

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          L 1 Reply Last reply
          0
          • R RomTibi

            Reviewing some old projects I met a function like this:

            bool func()
            {
            bool var = false;

            if(...)
                return var = false;
            else
                return var = true;
            

            }

            Actually what the function returns? I think true, because assigning can't be false.

            VII. 36. When you surround an army, leave an outlet free. Do not press a desperate foe too hard. SUN-TZU - Art of War

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

            RomTibi wrote:

            because assigning can't be false.

            Of course it can. This is easy to test by writing a few lines of code.

            1 Reply Last reply
            0
            • D David Crow

              RomTibi wrote:

              I think true, because assigning can't be false.

              True (regardless).

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

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

              Try it.

              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