title: Powershell: Why doesn't this function work?
body: <p>I am trying to make a script with neatly laid out functions that compare two folders items. The program:</p>
  
  <ol>
  <li>Prompts the user for the file path</li>
  <li>Check to see if the file names differ</li>
  <li>Check to see if the file sizes differ</li>
  </ol>
  
  <p>As a test I've been comparing the same folder to itself (output should be false, false). When making step 1 (<code>$referencepath</code>) a function (<code>FolderPrompt</code>) my program doesn't work right and by that I mean I seem to get a different answer almost every time I run it.</p>
  
  <p><strong>This works:</strong> </p>
  
  <pre><code>$referencePath = Read-Host -Prompt "Enter new DTNA folder path to check" 
  
  NameDisc
  SizeDisc
  
  function NameDisc {
      write-host "Name Discrepancy: " -NoNewline 
  
      if (Compare-Object -Property name (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
          {return $true} 
      else
          {return $false}
  }
  
  function SizeDisc {
      write-host "Size Discrepancy: " -NoNewline 
  
      if (Compare-Object -Property length (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
          {return $true} 
      else
          {return $false}
  }     
  </code></pre>
  
  <p><strong>But this does not:</strong></p>
  
  <pre><code>FolderPrompt
  NameDisc
  SizeDisc
  
  function FolderPrompt {
      $referencePath = Read-Host -Prompt "Enter new DTNA folder path to check" 
  }
  
  function NameDisc {
      write-host "Name Discrepancy: " -NoNewline 
  
      if (Compare-Object -Property name (Get-ChildItem $referencePath) -DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
          {return $true} 
      else
          {return $false}
  }
  
  function SizeDisc {
      write-host "Size Discrepancy: " -NoNewline
  
      if (Compare-Object -Property length (Get-ChildItem $referencePath) - DifferenceObject (Get-ChildItem P:\DTNA_201805081923))
          {return $true} 
      else
          {return $false}
  }     
  </code></pre>
  
  <p><strong>I've tried:</strong></p>
  
  <ul>
  <li><p>Declaring the functions before calling them</p></li>
  <li><p>Putting <code>$referencePath = 0</code> to reset the value each time
  thinking that was the problem</p></li>
  <li><p>Putting <code>Return $referencePath</code> at the end of different functions</p></li>
  </ul>
  
  <p>My best guess is that I need to do something like <code>function &lt;name&gt; ($referencePath)</code> to pass the variable(?).</p>