Search⌘ K
AI Features

Creating and Calling a Function

Explore how to create and invoke functions in both Python and PowerShell. Understand function definitions, parameters, use of docstrings in Python, and differences like indentation versus braces. This lesson will help you write functions clearly and call them properly using each language's conventions.

We'll cover the following...

Creating a Function

Before we can even use a function, we have to first define it with a name, a set of parameters, and the code block. This is called the function definition. Both Python and PowerShell follow different syntax and keywords, which are as follows.

Python Syntax:

def function_name (one or more comma separated parameters):
    "function docstring"
    function statements
    return <expression>

PowerShell Syntax:

function function_name (one or more comma separated parameters){
    <#
    .SYNOPSIS
    function help documentation
    #>
    function statements
    return <expression>
}

By default, both in Python and PowerShell parameters are positional, meaning that arguments will bind in the same order that they were defined. The following are a couple of rules to keep in mind when defining a function:

...