| by Arround The Web | No comments

Function return value in PowerShell

PowerShell has the host application “PowerShell ISE”, which is used to create scripts and functions. The function contains the statements or instructions specified by the user. Moreover, the function name is assigned by the user. These pieces of code are executed in PowerShell by calling the function name. According to your requirements, you can handle the complexity of a function.

This article will discuss the usage of the PowerShell return value.

What is the Function “return” Value in PowerShell?

return” is a value that the function returns to the calling function or a script when it completes its task. PowerShell uses the return value in functions to exit the script or function after outputting the result. The return keyword prevents the code after it from being executed. The return value in PowerShell stops the further execution of the code inside the function and returns the value as an output.

You can better understand how the return value is used by looking at the examples provided.

Example 1: Using Simple “return” Value

This example will output the text to the PowerShell console by utilizing the simple “return” value:

function TestReturn{
"This line will execute"
return
"This line will not execute"
}
TestReturn

 
In the above-given code:

    • First, we declared a function and then add the required message.
    • After that, we added the “return” keyword.
    • Finally, specify the other line which is not going to be executed in our case:


As you can see from the output, the code after the “return” value did not execute because the return value instantly stops the code and outputs the value to the console.

Example 2: Using “return” to Return the Sum of Values in PowerShell

In this example, we will add the two integers and execute them using the “return” keyword:

function TestReturn($a, $b){
write-output "This will return the output"
return ($a+$b)
write-output "This will not execute"
}
TestReturn 1 2

 
According to the given script:

    • First, we have defined a function named “TestReturn” with two arguments “$a” and “$b”.
    • After that, we added the text using the “write-output” cmdlet and then used the “return” keyword to add the values.
    • Then, add another text statement.
    • Lastly, invoke the created function by passing the required parameters:


It can be observed that firstly, the line added above the return statement has displayed the added message. Then, the sum of the passed variable has been returned, and the script got terminated.

Conclusion

return” value in the PowerShell function exits the script after the execution of the code above the return value. The code after this statement does not get executed. It is normally used to get the instant output of the given function. This tutorial has presented a detailed guide about the usage of the return value in the PowerShell function.

Share Button

Source: linuxhint.com

Leave a Reply