Skip to content

Clarify function calling syntax #4168

@RossBoylan

Description

@RossBoylan

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:

  1. Syntax for calling a function.
  2. 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.
  3. Can functions, including filters, that go in pipelines be defined with parameters? What happens if they are?
  4. What is the allowable syntax for names of functions and parameters?
  5. 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.
  6. 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?
  7. Is $input defined in the process block? Is $_ defined in the other blocks?
  8. Your example shows, I think, that $input has only the unprocessed inputs. Maybe make that explicit.
  9. 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.Count of 2. But if I call f($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.

Metadata

Metadata

Assignees

Labels

area-languageArea - PowerShell syntax and keywords

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions