-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
It would be very useful for this page to say more about how to call a function. Minimally, note that f(x, y) is not the right way to make a call. That's point 1 below, followed by other nits:
- Syntax for calling a function.
- The reference near the top to getting parameters from a pipeline is unclear. It seems to imply function arguments can come from the pipeline, but the later discussion only mentions $_ and $input.
- Can functions, including filters, that go in pipelines be defined with parameters? What happens if they are?
- What is the allowable syntax for names of functions and parameters?
- The reference to sending output to a pipe could explain exactly how that can be done. There seem to be several ways: as a return value; as a result of intermediate statements (not sure how it decides which statements count, since it doesn't seem to be all); maybe the result of write-output.
- For a function with begin/process/end blocks, are variables shared between blocks? Concretely, if the begin block sets up $a, will later blocks have access to it?
- Is $input defined in the process block? Is $_ defined in the other blocks?
- Your example shows, I think, that $input has only the unprocessed inputs. Maybe make that explicit.
- The following text was a bit confusing, because it sounded as if $input was the same as $_. It did later become clear it wasn't.
When you use a function in a pipeline, the objects piped to the function are assigned to the $input automatic variable.
The details of 4 may not belong here (though it would be nice if they were somewhere), and some of the issues about pipes might go better in the discussion of pipes.
5 is my current interest since I want to split an object in the pipe: an individual objects comes in, but multiple objects go out. I'm not sure if that's even possible, though I think with write-output it is.
But 1) is my main concern. Some additional comments on it:
- It would be better if the f(x, y) syntax just worked.
- Even if that were fixed, people using older versions will still be stuck.
- I, and I think many others, found the behavior really surprising and spent a couple hours debugging before I realized what was going on.
- In my searches I found some references to the f(a, b) syntax as working for functions that were defined in (maybe) .NET--maybe even being mandatory there.
- It's still unclear to me what is going on. When $a and $b are hash tables,
$r = @($a, $b)produces$r.Countof 2. But if I callf($a, $b)and evaluates the counts of $a and $b inside the function, I get the sum of the counts of a and b in the caller for a in the function, and 0 for b in the function
.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: d3a3fb40-0e44-40a0-c43c-7c4db52e7b39
- Version Independent ID: c7397f10-91d3-c735-244f-bfaa1072545a
- Content: about_Functions
- Content Source: reference/6/Microsoft.PowerShell.Core/About/about_Functions.md
- Product: powershell
- Technology: powershell-cmdlets
- GitHub Login: @SteveL-MSFT
- Microsoft Alias: slee