Powershell basics part 7 – Functions


Hello everyone,
In today’s post we will talk about functions, what are they and how can influence our scripting technique. First of all what is a functions? a function is a group of code lines that is seen as one entity. A function has a name, usually a name that describes what the function is doing for better understanding, and that function can be called from any location in our scripts. In practice functions help reduce code lines that are needed to a script. In a function you can group several lines of code and then you can recall those again and again just by calling the function’s name.
Function’s syntax : functions are declared pretty easy, you just have to type function followed by the name of the function and after that, the code:
function function-name
 {
  code
 }

I made a simple example of a function that calculates the sum of two numbers:

Functions in Powershell
As you can see in the image I have declared three variables a,b and s. Variable s will contain the sum of a and b. I’ve created the function called sum and inside it I actually calculated the sum and wrote it on the screen. Then I can call the function simple by typing it’s name and the result is displayed.
Remember that a functions have to be initialised before they are called upon. In the example if I called the function before I have created it, powershell would not know what to do.
 In our example we declared the numbers first and then we did the calculations. Function can also pass arguments, I will show you what I mean:
In our example we declared a,b and s before the function. Instead we can do the following:
Powershell function
In this example the two variables a and b are passed as arguments in the function, therefore we can call the function sum with any values that we want. Functions can also be called upon by specifying the arguments values. The following example will show you what I mean:
Powershell function
Also we can use the same function without specifying the variables in the function initialisation. We can do this by using the $args[] variables. They are inbuilt and they refer to the arguments that are passed to the function. Our example can be written as:
powershell function argument
In our example we have passed two arguments to the function, $args[0] and $args[1]. We can also specify in the function what types of variables do we use by writing param and the variable types. You can see what I mean in the following example:
Param Powershell
As you can see the two variables that we use, a and b are integer. Functions can also return a value. In our example we have written the answer by typing Write-Host “The sum equals:” $s. The same thing would happen if the variable $s would be written in the end of the function:
Powershell function
If you run this code you will see that the function returns the same thing.
OK, this is all about functions, in the next tutorials we will discus more complex things. I hope you’ve enjoyed it, have a nice day.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s