Discussion:
How does return work in powershell functions
(too old to reply)
meissnersd
2007-11-18 17:32:00 UTC
Permalink
Hey script quys.
I am trying to understand the return values for functions in Powershell

I defined a simple function in a script file.


function foo( [string] $ss ) {
$sb = new "System.Text.StringBuilder"
$sb.Append( $ss )
return $sb
}


I dot sourced it and then
. .\test.ps1
$x = foo "abc"
$x
Capacity
MaxCapacity Length
--------
----------- ------
16
2147483647 1
16
2147483647 1

$x is an array of objects.
First I returned the string builder, not an array....
Second the string builder is in the array twice.
Huh?
Shay Levi
2007-11-18 18:01:47 UTC
Permalink
"Function Output Consists of Everything That Isn't Captured".
For a deep understanding of your problem, I recommend reading Kieth Hill's
Effective PowerShell
posts series, start with Item 7: Understanding "Output", It has exactly what
you need.

http://keithhill.spaces.live.com/Blog/cns!5A8D2641E0963A97!811.entry

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Post by meissnersd
Hey script quys.
I am trying to understand the return values for functions in
Powershell
I defined a simple function in a script file.
function foo( [string] $ss ) {
$sb = new "System.Text.StringBuilder"
$sb.Append( $ss )
return $sb
}
I dot sourced it and then
. .\test.ps1
$x = foo "abc"
$x
Capacity
MaxCapacity Length
--------
----------- ------
16
2147483647 1
16
2147483647 1
$x is an array of objects.
First I returned the string builder, not an array....
Second the string builder is in the array twice.
Huh?
Keith Hill [MVP]
2007-11-18 18:05:28 UTC
Permalink
Post by meissnersd
Hey script quys.
I am trying to understand the return values for functions in Powershell
This is a common problem that folks with more traditional programming
experience when using functions in shell languages like PowerShell (or Korn
shell). I have a series of blog posts that explain this issue. You can
read them here:

Effective PowerShell Item 7: Understanding "Output"
http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!811.entry

Effective PowerShell Item 8: Output Cardinality - Scalars, Collections and
Empty Sets - Oh My!
http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!816.entry

--
Keith
meissnersd
2007-11-21 14:09:00 UTC
Permalink
Thanks Keith!
That was really helpful
Now I will have to read the rest of your blog :-)

btw I noticed the "vote for better loggin" links at the bottom of the blog
were pointing to dead pages.

I would add another option to vote for "typing 'help return' in powershell
tells you about this issue." Documentation on this seems pretty key.
Especially since it will surprise folks with programming backgrounds and
since you said a lot of people post this question over and over again.
Keith Hill [MVP]
2007-11-21 17:15:55 UTC
Permalink
Post by meissnersd
Thanks Keith!
That was really helpful
Now I will have to read the rest of your blog :-)
btw I noticed the "vote for better loggin" links at the bottom of the blog
were pointing to dead pages.
You need to establish a login on the Microsoft Connect site which just
requires logging in with a Windows Live ID (or .NET Passport ID). Then the
links will work.
Post by meissnersd
I would add another option to vote for "typing 'help return' in powershell
tells you about this issue." Documentation on this seems pretty key.
Especially since it will surprise folks with programming backgrounds and
since you said a lot of people post this question over and over again.
Once you are on the Connect site you should be able to submit bugs and
defects against PowerShell. If you feel particularly strongly about an
issue, post a link to the newsgroup and see if you can get other folks to
vote on it.

--
Keith
Bob Butler
2007-11-21 17:33:04 UTC
Permalink
Post by Keith Hill [MVP]
This is a common problem that folks with more traditional programming
experience when using functions in shell languages like PowerShell (or
Korn shell).
My suggestion for this would be to leave the default behaviour of returning
all non-captured values *unless* return statement is encountered. In that
case return only what is explicitly specified.

for example:
function noret {
1
2
3
}

returns an array of 3 values

function withret {
1
2
3
return 4
}

returns just the value 4

That would let the non-programmer users continue to get what they expect and
allow us anal-retentive programmer types to get what we expect from it as
well <g>

If it's too far gone for that now then add a "returnonly" keyword that
flushes the non-captured values. I can't tell you how much time I've lost
tracking down bugs because something returned a value that I hadn't expected
or cared about.
Keith Hill [MVP]
2007-11-21 17:40:54 UTC
Permalink
Post by Bob Butler
My suggestion for this would be to leave the default behaviour of
returning all non-captured values *unless* return statement is
encountered. In that case return only what is explicitly specified.
function noret {
1
2
3
}
returns an array of 3 values
function withret {
1
2
3
return 4
}
returns just the value 4
That would let the non-programmer users continue to get what they expect
and allow us anal-retentive programmer types to get what we expect from it
as well <g>
If it's too far gone for that now then add a "returnonly" keyword that
flushes the non-captured values. I can't tell you how much time I've lost
tracking down bugs because something returned a value that I hadn't
expected or cared about.
Yeah I don't think they could change the default behavior now but I really
like the idea of a returnonly keyword. Post on the Connect site as a
suggestion and I'll vote for it.

http://connect.microsoft.com

--
Keith
Bob Butler
2007-11-21 18:28:06 UTC
Permalink
Post by Keith Hill [MVP]
Yeah I don't think they could change the default behavior now but I really
like the idea of a returnonly keyword. Post on the Connect site as a
suggestion and I'll vote for it.
http://connect.microsoft.com
Sorry, no way in this lifetime I'll ever sign up for a Windows Live ID
Loading...