Scope/Function names and labels: Difference between revisions

Content added Content deleted
No edit summary
Line 490: Line 490:
Functions can be compiled separately, and then linked with a program
Functions can be compiled separately, and then linked with a program
in which case they are globally accessible.
in which case they are globally accessible.
</lang>

=={{header|PowerShell}}==
A function exists in the scope in which it was created.

If a function is part of a script, the function is available to statements within that script. By default, a function in a script is not available at the command prompt.

You can specify the scope of a function. For example, the function is added to the global scope in the following example:
<lang PowerShell>
function global:Get-DependentService
{
Get-Service | Where-Object {$_.DependentServices}
}
</lang>
When a function is in the global scope, you can use the function in scripts, in functions, and at the command line.

Functions normally create a scope. The items created in a function, such as variables, exist only in the function scope.

For more information about scope in Windows PowerShell, see about_Scopes
<lang PowerShell>
Get-Help about_Scopes
</lang>
</lang>