1

I can't find an example. How do I make a prototype for my powershell functions?

function exampleFunc(); //other stuff that calls the example function Function exampleFunc(){//stuff}

  • There are examples on the [SO Docs](http://stackoverflow.com/documentation/powershell/1673/powershell-functions#t=201607281323139314936) – 4castle Jul 28 '16 at 13:23
  • I mean like `function exampleFunc();` `other stuff that calls the example function` `Function exampleFunc(){//stuff}` – Michael Ellis Jul 28 '16 at 13:25
  • 2
    You can't do that. Powershell isn't a compiled language. – 4castle Jul 28 '16 at 13:27

3 Answers3

6

PowerShell doesn't support prototype functions, forward declarations or whichever term you want to use for this. In PowerShell, when you use the function keyword you're defining a function. If you call it twice with the same function name, you change the function's definition.

This question about the same issue with bash lists the common methods to get around the issue. You can do the same things in PowerShell.

Another option is to use the Begin {} Process {} End {} advanced function construct, and put all your function declarations in the Begin {} portion.

Community
  • 1
  • 1
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
0

You could call for snippets from Powershell ISE (Integrated Scription Environment) by right click or access via quick buttons Ctrl+J.

Kirill Pashkov
  • 3,118
  • 1
  • 15
  • 20
0

And here is what @bacon-bits code would look like

function main()
{
    cls
    Begin
    Process
    End
}

function  Begin {

}

function  Process {

}

function  End {

}

main
Raj Rao
  • 8,872
  • 12
  • 69
  • 83
  • prototyping a function would be a very useful feature to powershell. I don't like to think about ordering by functions for usage. – Golden Lion Jan 06 '21 at 16:10