$result vrs return

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Shane Lindberg Shane Lindberg 1 year, 5 months ago.

  • Author
    Posts
  • #28116
    Profile photo of Shane Lindberg
    Shane Lindberg
    Participant

    I have been reading a Powershell for beginners book. It talks about using $result to return data from a function. I noticed Powershell also uses "return" to return data from a function

    As far as I can tell the difference between the two is "$result" returns data but does not exit the function, where "return" both returns data and exits the function.

    Am I understanding this correctly?

  • #28117
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Kind of neither :). Powershell isn't a traditional programming language; apart from classes in v5, the Return keyword is kind of an anomaly. Any output of a function – output from any commands that isn't captured, as well a anything produced by write-output – is the actual output of the function. Not sure what book you're reading, but it doesn't sound like it's really teaching you the shell's native patterns. If you start treating it like a programming language, which is what those two approaches do, it's easy to get confused because you're not aware of the stuff going on under the hood. Powershell functions are intended to work in the pipeline, which is the main mechanism for passing data between units of execution.

  • #28120
    Profile photo of Shane Lindberg
    Shane Lindberg
    Participant

    I am trying to sink what you said in. Below is how I got my code to work, but maybe it is not the preferred way. Would you suggest another way? Would it make make sence to use write-output along with return? Any suggestions would be appreciated

    this is the computer algorithm for a tic tac toe game

    # this is the grid pattern used for moves. 4 is the center move
    #012
    #345
    #678
    
    #for testing some values were filled in for the moves
    $usedmoves = (0,4,6,8,2)
    $computermoves = (0,8,2)
    $humanmoves = (6,4)
    
    function computer-move {
    
        $bestmoves = ( 4,0,2,6,8,1,3,7,5 )
        $win = ((0,1,2),(0,4,8),(0,3,6),(2,4,6),(2,5,8),(1,4,7),(3,4,5),(6,7,8))
        
    
        #this first part checks if the computer can go for a win
        #if the win is possible it returns that data
        foreach ($i in $win){
            $check=@()
            if ($i[0] -in $computermoves){
                $check+=$i[0]
            }
            if ($i[1] -in $computermoves){
                $check+=$i[1]
            }
            if ($i[2] -in $computermoves){
                $check+=$i[2]
            }
            if ($check.count -eq 2){
                foreach ($k in $i){
                    if (($k -notin $computermoves) -and ($k -notin $usedmoves)){
                        $result = $k 
                        $result
                        return
                    }
                }
            }
        }
        #the second most important move is to block any wins.
        #this checks for any near wins by the opponent and blocks them
    
        foreach ($i in $win){
            $check = @()
            if ($i[0] -in $humanmoves){
                $check+=$i[0]
            }
            if ($i[1] -in $humanmoves){
                $check+=$i[1] 
            }
            if ($i[2] -in $humanmoves){
                $check+=$i[2] 
            }
            if ($check.count -eq 2){
                foreach ($k in $i){
                    if (($k -notin $humanmoves) -and ($k -notin $usedmoves)){
                        $result = $k
                        $result
                        return
                    }       
                }
            }
        }
        #if the computer can't win or does not need to block a win it will 
        #pick its best move by starting with the center, then corners, then sides
        
        foreach ($i in $bestmoves){
            if ($i -notin $usedmoves){
                $result = $i 
                $result
                return
            } 
        }
    }
    
    
    $a = computer-move
    Write-Host $a
    
  • #28122
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    OK so you want to return from the function as soon as you have a result. Your code appears to do that. You make a bit neater by replacing

    $result = $k
    $result
    return

    with

    return $k

    The code you had
    $result = $k – – sets the variable result to the value of $k
    $result — puts $result on the pipeline i.e. outputs your data
    return — terminates the use of the function

    I'd suggest reading the about_return help file.

    Just out of interest which book are you reading?

  • #28123
    Profile photo of Shane Lindberg
    Shane Lindberg
    Participant

    Windows Powershell Programming for the Absolute Beginner 3rd edition

You must be logged in to reply to this topic.