RETURN keyword

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Olaf Soyk Olaf Soyk 2 weeks, 5 days ago.

  • Author
    Posts
  • #72274
    Profile photo of Hermann
    Hermann
    Participant

    In an interview Jeffrey Snover was asked:
    "Seeing the way PowerShell has evolved, what is your biggest regret?"
    He answered:
    "The return keyword was a misstep. Because any command output prior to return is also included, it doesn't behave intuitively.
    ..." ( http://www.brianbunke.com/blog/2017/04/11/snover-ama/ )

    Can anybody explain this, with some examples?

  • #72275
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    In other programming languages returns a function only what's specified with the key word "return". In Powershell a function returns everthing what's left in the "output channel".

    function Return-everything {
        'Simple string'
        'Another simple string'
    
        Return 'Return should only return what comes after the key word return, right?'
    }
    

    This function in other languages would only return the content of the last line. Try it! 😉

You must be logged in to reply to this topic.