Topic
  • 8 replies
  • Latest Post - ‏2014-02-26T19:45:20Z by llandale
bandchef
bandchef
21 Posts

Pinned topic Return-Value Problem

‏2014-02-24T08:59:19Z |

Hi!

I have written this code in the attachement. The 2 functions must have a bool return-value.

The Compiler says me, that function 'identifyFunction' is terminated with this Error: "function terminated without executing return Statement".

 

Attachments

  • MichaelGeorg
    MichaelGeorg
    53 Posts

    Re: Return-Value Problem

    ‏2014-02-24T10:14:57Z  

    Hello bandchef,

    you just need to add a return statement after the "if(ht == modifyObject)" part. As in case ht is not modifyObject currently your identifyFunction doesn't return a value.

     

     - Michael

  • bandchef
    bandchef
    21 Posts

    Re: Return-Value Problem

    ‏2014-02-24T10:23:17Z  

    Hello bandchef,

    you just need to add a return statement after the "if(ht == modifyObject)" part. As in case ht is not modifyObject currently your identifyFunction doesn't return a value.

     

     - Michael

    Thank you for your answer!

    What do you exactly mean by writing: "you just need to add a return statement after the "if(ht == modifyObject)" part."? What is a return Statement? I think, that if the if-statement is true, the true block will return a 'true' by using the statement "return true", isn't it?

  • M_vdLaan
    M_vdLaan
    31 Posts

    Re: Return-Value Problem

    ‏2014-02-24T11:17:25Z  
    • bandchef
    • ‏2014-02-24T10:23:17Z

    Thank you for your answer!

    What do you exactly mean by writing: "you just need to add a return statement after the "if(ht == modifyObject)" part."? What is a return Statement? I think, that if the if-statement is true, the true block will return a 'true' by using the statement "return true", isn't it?

    Bandchef,

     

    The thing is, if ht != modifyObject, the function does not return a value. What should identifyFunction return in this case?

    The current else clause only works for th inner condition, not the outer condition.

     

    -Marcel

     

     

  • bandchef
    bandchef
    21 Posts

    Re: Return-Value Problem

    ‏2014-02-24T12:30:52Z  

    Ah, thank you Marcel!

    The function should return a false in case of ht != modifyObject. The problem now is, that the function always returns false.

    Attachments

  • MichaelGeorg
    MichaelGeorg
    53 Posts

    Re: Return-Value Problem

    ‏2014-02-24T12:55:40Z  
    • bandchef
    • ‏2014-02-24T12:30:52Z

    Ah, thank you Marcel!

    The function should return a false in case of ht != modifyObject. The problem now is, that the function always returns false.

    Code of identifyFunction now looks fine to me, so perhaps the problem is in skip you are using.

    Or the problem is in line 57, where you have a return in your loop through the history items. This way you will always only process the first history item, is this your intention?

     - Michael

  • M_vdLaan
    M_vdLaan
    31 Posts

    Re: Return-Value Problem

    ‏2014-02-24T15:50:29Z  
    • bandchef
    • ‏2014-02-24T12:30:52Z

    Ah, thank you Marcel!

    The function should return a false in case of ht != modifyObject. The problem now is, that the function always returns false.

    As Michael stated, the code round line 57 doesn't look right. If you're checking for changes you'd probable want to return true here only in the identifyFunction returns true. It looks as though there's some efficiency to be won in this piece of code, as well.

    There's another possible gotcha in this code: are you aware that the "for str_Attribute in s do" line iterates over the values of the skip list, not over the keys? Not knowing what's in you skiplist makes it hard to decode.

    A guess at what you're trying to achieve (assuming the attributes are the keys in the skiplist):

    bool checkForChanges(Module m, Skip s)
    {
      History h
      for h in m do {
        if (h.type == modifyObject) {
          if (find(s, h.attrName))  {
            return true
          }
        }
      }
      return false
    }
    
    Skip s = createString
    put(s, "Object Text", "Any old value, doesn't matter")
    put(s, "Object Short Text", 1)
    
    if (checkForChanges(current Module, s)) {
      print "Module changed in one of the attributes\n"
    }
    else {
      print "Module not changed\n"
    }
    

    Good luck.

  • llandale
    llandale
    3035 Posts

    Re: Return-Value Problem

    ‏2014-02-26T19:39:55Z  
    • bandchef
    • ‏2014-02-24T12:30:52Z

    Ah, thank you Marcel!

    The function should return a false in case of ht != modifyObject. The problem now is, that the function always returns false.

    I presume "checkForChanges()" should return "true" when even a single qualifying History was detected.

    Change line 57:

    • from "return b_changeFlag"
    • to "if (b_changeFlag) then return(b_changeFlag)    // A qualifying History was found

    Insert a new line after #59:

    • return(false)     // No qualifying History was found

    M_vdLaan is probably right about an error in your Key/Data values in the Skip (good catch).  Let's find out:

    Change line #49

    • from "{"
    • to "{ string KEY = (string key s)"

    Change line #51

    • from "//print "The current Attribute: " str_Attribute "\n\n"
    • to:   "print "The current Attribute: key [" KEY "]\t str_Attribute: [" str_Attribute "]\n"

    Yes, the Skip "find" function searches for the "Key", whereas the Skip loop loops through the "data".  The "key" function (my new line #49) works inside such a loop and is pretty routine when dealing with Skip loops.  Note it must be "caste", which is the "string" word in that line.  Note that the interpreter will correctly flag an error if the "key" function is used inside the wrong loop.

    -Louie

    Even after all these years and 1000s of "Skips" I still get confused (it's one of my several conceptual "black holes").  So I declare all my Skips with a comment explicitely stating what is the "Key" and what is the "Data"; like this:

    • Skip skpObjects = createString()   // KEY 'string' Object Identifier; DATA: 'Object' handle of that object

    I routinely find all references in the code to "skpObjects" (create, put, get, and loop) and make sure I'm dealing with it as specified in the comment.  Errors associated with using the Skip inconsistently are particularly hard to find, at least for me.

     

  • llandale
    llandale
    3035 Posts

    Re: Return-Value Problem

    ‏2014-02-26T19:45:20Z  
    • bandchef
    • ‏2014-02-24T12:30:52Z

    Ah, thank you Marcel!

    The function should return a false in case of ht != modifyObject. The problem now is, that the function always returns false.

    Scattering "return" statements around inside a function can get confusing.  In this case, you can drastically improve function "identifyFunction" with this:

    • if (ht == modifyObject and h.attrName == str_Attribute) //
    • then return(true)
    • else return(false).

    Or this:

    • return(ht == modifyObject and h.attrName == str_Attribute)

    "h.attrName" will trigger a run-time error unless "ht" is of the correct type (modifyObject is one of the correct types).  Thus, reversing the order will not work:

    • return(h.attrName == str_Attribute and ht == modifyObject)

    -Louie

     

    -Louie