Databases Reference
In-Depth Information
Figure 4-4
Returning Values
Typically, a function should return values so that the calling program can manipulate or make use
of the returned values.
The following example creates a compute function that can add, delete, multiply, or divide based
on the parameter. Then it returns the computed value to the calling script. Create this in a Power-
Shell script calculate-arith1.ps1 , and save the script file under the directory C:\DBAScripts .
The script passes 1 ,
,and 20 to the function, The function adds 1 to 20 and passes the result 21
back to the script, and then the script prints out 21:
+
# ==================================================
#
# NAME: calculate-arith1.ps1
#
# AUTHOR: Yan and MAK
# DATE : 4/26/2008
#
# COMMENT: Demo for "Return value"
#
# ==================================================
Function compute([int] $x ,[string] $y, [int] $z)
{
switch ($y)
{
" + " { $computed = $x + $z }
"-" { $computed = $x-$z }
"/" { $computed = $x/$z }
"*" { $computed = $x*$z }
"%" { $computed = $x%$z }
}
return $computed
}
$a = compute 1 " + "20
write-host $a
Search WWH ::




Custom Search