Nesting functions

In the same way that a script can contain functions, a function can contain other functions. This is shown in the following example:

function Outer {
param (
$Parameter1
)

function Inner1 {
}
function Inner2 {
}

Write-Host 'Hello world'
}

This technique can be used to isolate small repeated sections of code with a function.

Nested functions must appear before they are used, but otherwise can appear anywhere in the body of the function.

The disadvantage of nesting a function in this manner is that it becomes much harder to test as a unit of code. The function only exists in the context of its parent function; it cannot be called from the scope above that. This is an important consideration when developing a function as part of a module.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.54.6