title: Powershell functions executed regardless of being called
body: <p>I'm having trouble understanding how Powershell treats functions. In the following script all functions are called, even if I never actually call the main function. Does powershell not have a concept of call chain?</p>
  
  <pre><code>param([string]$directory)
  
  [string]$global:sqlscript;
  $global:types = @{
  "double" = "DOUBLE PRECISION"; 
  "int" = "INTEGER"; 
  "QString" = "INTEGER";
  "Ignored" = "1";
  "Normal" = "2";
  "Critical" = "3" }
  
  function resultToSql($element)
  {
    $global:sqlscript += ('"')
    $global:sqlscript += ($element.name + '" ')
    $global:sqlscript += ($global:types.Get_Item($element.type))
    $global:sqlscript += (',' + [Environment]::Newline)
    $global:sqlscript += ('"' + $element.name + "_metric_group" + " " + $global:types.Get_Item($element.metric_group.type))   
    $global:sqlscript += (',' + [Environment]::Newline)
  }
  
  function xmlToSql($source)
  {
    Write-Host "Parsing...";
    $global:sqlscript += "CREATE TABLE IF NOT EXISTS " + '"' + $source.spec.task.ToLower() + '"'+ [Environment]::NewLine
    $global:sqlscript += '"' + "id" + '"' + " SERIAL NOT NULL" + [Environment]::NewLine
  
    foreach ($node in $source.spec.measure) {
        resultToSql $node
    }
  
    foreach ($m in $source.spec.error) {
      resultToSql $m
    }
  
    $global:sqlscript += '"' + "weighted_sum" + '" ' + $global:types.Get_Item("double") + [Environment]::Newline;
  }
  
  function main
  {
    if ($directory -eq $null) { exit }
    else
    {
      $xmlfiles = Get-ChildItem -Path $directory -include *Spec.xml
      foreach ($xmlfile in $xmlfiles)
      {
          Write-Host "Filename:" $xmlfile;
          [xml]$spec = Get-Content $file;
          xmlToSql $spec; 
          Write-Host $script;
      }
    }
  }
  </code></pre>