![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Returning Variables from a Function... Is it possible to return 2 variables from a function? Somethig like this.....? Function DoThis ( $inString, $outString1, $outString2) { ..... Do some stuff $outString1 = $a $outString2 = $b } DoThis $inputToFunction $outputFromFunction1 $outputFromFunction2 $outputFromFunction1 $outputFromFunction2 Does that make sense....!? doubt it. Basically however it's done, I like to call a function that takes one input, and returns 2 or 3 outputs to assigned variables. Cheers Russ. |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Returning Variables from a Function... Anything that is not captured (assigned to a variable) inside the function is returned. Check out Effective PowerShell Item 7: Understanding "Output" http://keithhill.spaces.live.com/Blo...3A97!811.entry ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Quote: > Is it possible to return 2 variables from a function? > > Somethig like this.....? > > Function DoThis ( $inString, $outString1, $outString2) > { > .... Do some stuff > $outString1 = $a > $outString2 = $b > } > > DoThis $inputToFunction $outputFromFunction1 $outputFromFunction2 > > $outputFromFunction1 > $outputFromFunction2 > Does that make sense....!? doubt it. > Basically however it's done, I like to call a function that takes one > input, and returns 2 or 3 outputs to assigned variables. > Cheers > Russ. |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Returning Variables from a Function... Actually you have to try to control what you return. Function in Powershell are not like functions in VBScript. In Powershell anything that is outputed will get returned from the function. So lets take this example function foo {"Value1","Value2"} $v1,$v2 = foo $v1 Value1 $v2 Value2 An exception is if you use Write-Host, Write-Verbose, Write-Error... These write data to console and not output. Make sense? Brandon Shell --------------- Blog: http://www.bsonposh.com/ PSH Scripts Project: www.codeplex.com/psobject r> Is it possible to return 2 variables from a function? r> r> Somethig like this.....? r> r> Function DoThis ( $inString, $outString1, $outString2) r> { r> .... Do some stuff r> $outString1 = $a r> $outString2 = $b r> } r> r> DoThis $inputToFunction $outputFromFunction1 $outputFromFunction2 r> r> $outputFromFunction1 r> $outputFromFunction2 r> Does that make sense....!? doubt it. r> Basically however it's done, I like to call a function that takes one r> input, and returns 2 or 3 outputs to assigned variables. r> Cheers r> Russ. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Returning Variables from a Function... Try this: help about_ref Is it what you want? -- Thanks, Roman Kuzmin http://code.google.com/p/farnet/ PowerShell and .NET in Far Manager "rush" <russell.hancock@xxxxxx> wrote in message news:01f7bd93-1beb-44d9-bed6-25c21241ea64@xxxxxx Quote: > Is it possible to return 2 variables from a function? > > Somethig like this.....? > > Function DoThis ( $inString, $outString1, $outString2) > { > .... Do some stuff > > $outString1 = $a > $outString2 = $b > > } > > DoThis $inputToFunction $outputFromFunction1 $outputFromFunction2 > > $outputFromFunction1 > $outputFromFunction2 > > Does that make sense....!? doubt it. > Basically however it's done, I like to call a function that takes one > input, and returns 2 or 3 outputs to assigned variables. > > Cheers > Russ. |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Returning Variables from a Function... as was mentioned everything not consumed in a function is returned.. so function myfunc { 1 2 3 4 5 } when you run that 5 items are returned.. if you do $a = myfunc then $a will contain an array with 5 itemes.. well powershell allows the syntax of assigning the results to more than one item.. so $a,$b,$c,$d,$e = myfunc will put 1 in $a, 2 in $b etc well what happens when there are more items returned than variables? basically the last item gets the remaining so $a,$b,$c = myfunc will result in $a containing 1 $b containg 2 and $c containing and array of 3 4 and 5. -Karl |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Returning Variables from a Function... Thanks Karl & Brandon... That's what I needed to know! |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Problem in returning an object from a function | VB Script | |||
| function not returning anything | PowerShell | |||
| Function returning a value | VB Script | |||
| Can I get two variables out of a function? | PowerShell | |||
| Parsing Text File and returning variables | PowerShell | |||