Creating and Calling a Function

Learn how to create and call a function in Python and Powershell.

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:

  • Python function blocks begin with the keyword def followed by the function name and parenthesis ( ). You can define the function parameters or arguments in the parenthesis and end the line with a colon (:).

    def function_name (one or more comma-separated parameters):
    

On the other hand, PowerShell function blocks begin with the keyword function and we end the line by opening a brace {.

```PowerShell
function function_name (one or more comma separated parameters) {
```
  • In Python, the first statement of a function is an optional statement, also known as the function’s documentation string docstring.

    def greet(name):
        """This function greets when run
        Print a greeting with the name passed as input parameter"""
        print('Hello {0}! from Python'.format(name))
    

To access the docstring of a function, we can simply provide the function name followed by (.) dot operator and __doc__ property.

PowerShell doesn’t support a docstring. Instead, we can add helpful information for documentation purposes by using a multi-line comment, like in the following code sample.

```PowerShell
function greet($name){
    <#
    .SYNOPSIS
    This function greets when run

    .DESCRIPTION
    Print a greeting with the name passed as input parameter
    #>
    "Hello {0}! from Python" -f $name
}
```

To view help description of this function you simply use the `Get-Help <Function Name>`
  • After the docstring, we can add all the statements that the functions are required to execute: logical statements, loops, etc. in the body of the function.

  • Indentation in Python is something to keep in mind while defining our functions. Python has a particular style of indentation to define the code since Python functions don’t explicitly begin or end using the curly braces to indicate the start and stop for the function body like in PowerShell. Python relies on indentation instead. This means we have to add indentation (space) in line-2 of the following code, because that is how Python understands that it is the beginning of the function body where we define all our statements that we want to execute when the function is called.

Get hands-on with 1400+ tech skills courses.