Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:42:39+00:00 2026-06-17T10:42:39+00:00

I am defining a function to switch from Player X to player O. When

  • 0

I am defining a function to switch from Player “X” to player “O”. When I run this little block of code without the function, it gives me back an X. When I run this with a function defined it returns as O. What makes the difference between running it normally and running it from a Function ?

$playgame = "True"
$player = "O"

#Function
Switch-play
Write-host $player



#Switch Player turn
Function Switch-Play{
    if ($playgame = "True") {
        if ($player -eq "X") {$player = "O"}
        else {$player = "X"}
        }
}

Thanks

EDIT: At first I was in doubt about defining the variables as $script:player, but that didn’t really solve anything.

EDIT: changing to Switch-Play rather than Switch-play

PS C:\Users\scout> $playgame = "True"
$player = "O"
$player
Switch-Play
$player
Switch-Play 
$player




#Switch Player turn
Function Switch-Play{
    if ($playgame = "True") {
        if ($player -eq "X") {$player = "O"}
        else {$player = "X"}
        }
}
O
O
O
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:42:40+00:00Added an answer on June 17, 2026 at 10:42 am

    Variable Scope issue here. Change function like this:

    Function Switch-Play{
        if ($playgame) {
            if ($global:player -eq "X") 
                {
                  $global:player = "O"}
            else 
                {                
                  $global:player = "X"
                }
            }
    }
    

    Reading about scope: http://technet.microsoft.com/en-us/library/hh847849.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I run the function below before defining it, I will get this error...
I want to do the following without defining a function: if isinstance(x,(list,tuple)) and every_element_isinstance(x,basestring):
Take the following code from CodeIgniter's show_error function: $_error =& load_class('Exceptions', 'core'); The documentation
I just ran into a problem when defining a function in a block scope.
What does the following javascript code mean? I guess it's defining a function within
I am defining this function in JavaScript and I am getting an error like
I am loading a function from a .SO 'plugin' file in my code. The
I am defining this function in one of my classes: public function onUse():void {};
What is the purpose of defining a function without statements? I have a C++
I'm defining a function in PowerShell and trying to call it as shown below.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.