Functions are little subroutines of code that are intended to be self-contained. Functions have their own scope, as outlined in the previous discussion on scope. Variables declared within a function are accessible only to the function. If a function is run as part of a script, then the function can access the script's scope because the script is the parent of the function.
Functions are declared with the keyword function, given a name, and then can include whatever code you need. For example:
function myFunction { $var = 3 $script:var = "Hello" }
Notice that the function's code is enclosed by {curly braces}, which lets PowerShell know where the function starts and stops.