How to return one and only one value from a PowerShell function?

boli

I've learned from this Stack Overflow question, that PowerShell return semantics are different, let's say, from C#'s return semantics. Quote from the aforementioned question:

PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around: All output is captured, and returned. The return keyword really just indicates a logical exit point.

Let's look at this example:

function Calculate
{
   echo "Calculate"
   return 11
}

$result = Calculate

If you echo $result you will realise that something is not right. You'd expect this:

11

But the reality, what you actually see, is different:

Calculate
11

So instead of getting back only the intended return value, you actually get back an array.

You could get rid of the echo statements and not polluting the return value, but if you call another function from your function, which echoes something, then you're in trouble again.

How can I write a PowerShell function that only returns one thing? If the built-in PowerShell functions do return only one value, why can't I do that?

The workaround that I'm using now and I'd love to get rid of:

function Calculate
{
   # Every function that returns has to echo something
   echo ""
   return 11
}

# The return values is the last value from the returning array
$result = (Calculate)[-1]
Michael Sorens

Several answers already given do not fully answer your question because they do not account for other cmdlets you might call--as you point out--that might "pollute" your output. The answer from @Laezylion is essentially correct but I believe bears further elaboration.

It is clear that you appreciate that your current workaround--forcing every function to return an array then taking the last element--is not a very robust solution. (In fact, I think it is safe to call that a kludge. :-) The correct approach--and a direct answer to your question--is clear... though at first glance it sounds like a tautology:

Question: How do you ensure a function returns only one thing?
Answer: By ensuring that it returns only one thing.

Let me elucidate. There are really two kinds of functions/cmdlets in PowerShell: (A) those that return data and (B) those that do not return data but may instead report progress or diagnostic messages. Problems arise, as you have seen, when you try to mix the two. And this may easily happen inadvertently. Thus, it is your responsibility, as the function author, to understand each function/cmdlet call within your function: specifically, does it return anything, be it a true return value or just diagnostic output? You are expecting output from type A functions, but you need to be wary of any type B functions. For any one that does return something you must either assign it to a variable, feed it into a pipeline, or... make it go away. (There are several ways to make it go away: pipe it to Out-Null, cast it to void, redirect it to $null, or assign it to $null. See Jason Archer’s Stack Overflow post that evaluates the performance of each of these flavors, suggesting you shy away from Out-Null.)

Yes, this approach takes more effort, but you get a more robust solution by doing so. For a bit more discussion on this very issue, these A/B functions, etc. see Question 1 on A Plethora of PowerShell Pitfalls recently published on Simple-Talk.com.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Python: How to obtain return value from only one function (out of several executed with asyncio.gather)

分類Dev

Accessing only one of the return values from a function

分類Dev

jQuery: How to pass value from one function to another?

分類Dev

How to compose functions that return Bools to one function

分類Dev

Function that returns one value from two columns

分類Dev

Passing variable value from one function to another

分類Dev

Trying to return one dataset from a function that prints from two functions

分類Dev

How to get the value for one function to another?

分類Dev

How to add two vectors with a scalar and save the result in one of the two vectors and return this vector from a function in C?

分類Dev

Cannot change value of @Binding, also how to animate a view from only one toggle state

分類Dev

How can I return true only if one of a set of strings matches?

分類Dev

how to echo only one value either the value is multiple [PHP]

分類Dev

Reading sh_name from header will return one value everytime

分類Dev

How to pass value from one dialog to another

分類Dev

How to assign value from one hashset to another?

分類Dev

How to get One value from an array (protractor)?

分類Dev

How to return value from debounced function in javascript?

分類Dev

How to correctly return value from function?

分類Dev

How to return value from a child process in a function?

分類Dev

How to get a return value from a connect function

分類Dev

How to return value from an extended casperjs function?

分類Dev

MySQL COUNT query returns only one value from a CASE expression

分類Dev

How to pass a value from one javascript function to another in html.erb

分類Dev

R: how to use the aggregate()-function to sum data from one column if another column has a distinct value?

分類Dev

DISTINCT only on one value with SPARQL

分類Dev

How to split only one row value and insert into multiple columns using stored procedure, parameter value is from C#

分類Dev

How to modify only one element in a react .map function?

分類Dev

How to get only one value in Javascript array of objects using for of and for in statements?

分類Dev

How to pass return value from one `npm run script.js` as a parameter to another `npm run script2.js`?

Related 関連記事

  1. 1

    Python: How to obtain return value from only one function (out of several executed with asyncio.gather)

  2. 2

    Accessing only one of the return values from a function

  3. 3

    jQuery: How to pass value from one function to another?

  4. 4

    How to compose functions that return Bools to one function

  5. 5

    Function that returns one value from two columns

  6. 6

    Passing variable value from one function to another

  7. 7

    Trying to return one dataset from a function that prints from two functions

  8. 8

    How to get the value for one function to another?

  9. 9

    How to add two vectors with a scalar and save the result in one of the two vectors and return this vector from a function in C?

  10. 10

    Cannot change value of @Binding, also how to animate a view from only one toggle state

  11. 11

    How can I return true only if one of a set of strings matches?

  12. 12

    how to echo only one value either the value is multiple [PHP]

  13. 13

    Reading sh_name from header will return one value everytime

  14. 14

    How to pass value from one dialog to another

  15. 15

    How to assign value from one hashset to another?

  16. 16

    How to get One value from an array (protractor)?

  17. 17

    How to return value from debounced function in javascript?

  18. 18

    How to correctly return value from function?

  19. 19

    How to return value from a child process in a function?

  20. 20

    How to get a return value from a connect function

  21. 21

    How to return value from an extended casperjs function?

  22. 22

    MySQL COUNT query returns only one value from a CASE expression

  23. 23

    How to pass a value from one javascript function to another in html.erb

  24. 24

    R: how to use the aggregate()-function to sum data from one column if another column has a distinct value?

  25. 25

    DISTINCT only on one value with SPARQL

  26. 26

    How to split only one row value and insert into multiple columns using stored procedure, parameter value is from C#

  27. 27

    How to modify only one element in a react .map function?

  28. 28

    How to get only one value in Javascript array of objects using for of and for in statements?

  29. 29

    How to pass return value from one `npm run script.js` as a parameter to another `npm run script2.js`?

ホットタグ

アーカイブ