• nickwitha_k (he/him)@lemmy.sdf.org
    link
    fedilink
    English
    arrow-up
    14
    arrow-down
    1
    ·
    edit-2
    21 days ago

    I’m pretty sure that funky() would always return 0, as defined. I’ll pseudocode that up:

    funky takes no args, returns int {
      a is assigned the value 0
      b is assigned the value 1
    
      test if a is equal to 1, if it is {
        b is assigned the value 1
      }
    
      return a
    }
    

    The if in your function can never be reached, without some weird manipulation of the value of a that breaks variable scoping in most syntaxes.

    I think that I see your logic but it is syntactically incorrect:

        if ( a==1 ) {
            b=1
        }
    

    In most syntaxes, this is a conditional execution and value assignment. That is, the code in curly braces only gets executed, if the conditional evaluates as true. If the conditional evaluates as true, the code is executed, assigning the value 1 to the variable b.

    It does NOT imply that the assignment of the value 1 to the variable b is a conditional requiring the assignment of the value 1 to the variable b.

    Remember: = in most programming is NOT an equality symbol but a value-assigment symbol. It would be nice if people creating the initial syntaxes used something else that is harder to confuse but they didn’t.

    • stevedice@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      5
      arrow-down
      1
      ·
      edit-2
      21 days ago

      Yes, I know, that’s the point. Funky is specifically constructed to always return 0. Then we assume “if” and “if, and only if” are equivalent and by following that assumption to its logical conclusion, we deduce that funky returns 1. Therefore, our assumption was incorrect because 0≠1. It follows that “if” isn’t equivalent to “if, and only if”. Also, it’s just a shitpost.

      • nickwitha_k (he/him)@lemmy.sdf.org
        link
        fedilink
        English
        arrow-up
        2
        ·
        21 days ago

        If reading the code as non-programming logic, that conclusion makes sense, yes. However, if, in most syntaxes, is a type of flow control. What it wraps has no meaning to the if statement itself. Reading it through the lens of an interpreter/compiler makes it clear. The statement is approximately:

        If and only if a is equal to 1, do the thing {
          The thing is: assign the variable b with the value 1
        }
        

        To one not familiar with how programs are executed, it would make sense that the return value could be 1. But understanding how flow control works in programming, makes this interpretation a challenge.

        • stevedice@sh.itjust.works
          link
          fedilink
          English
          arrow-up
          4
          ·
          21 days ago

          I don’t think you’re picking up what I’m putting down. I’m not arguing that the return value can be 1, I’m well aware that it can’t — I wrote the function so that it will always return 0. It only returns 1 if we make an incorrect assumption (and mix up semantics with formal logic, but that’s another conversation), the incorrect assumption being “if is equivalent to if, and only if