Functions
For any recurring set of instructions, we can define a function. In other words, a function is a closed set of instructions to perform a specific logic or task. Depending upon the input provided, a function has the ability to return the results or parse the input with specific instructions to get results without any return values.
A function is defined by the def keyword, which specifies that we need to define a function and provide a set of instructions related to that function.
In this task we will print the greater of two input numbers:
def checkgreaternumber(number1,number2):
if number1 > number2:
print ("Greater number is ",number1)
else:
print ("Greater number is",number2)
checkgreaternumber(2,4)
checkgreaternumber(3,1)
As we can see in the preceding output, the first time we call the checkgreaternumber(2,4) function, the function prints the greater value as 4, and the second time we call the function with different numbers, the function prints the greater value as 3.
The PowerShell sample code for the preceding task is as follows:
#PowerShell sample code
function checkgreaternumber($number1,$number2)
{
if ($number1 -gt $number2)
{
write-host ("Greater number is "+$number1)
}
else
{
write-host ("Greater number is "+$number2)
}
}
checkgreaternumber 2 4
checkgreaternumber 3 1
We can rewrite the same function, but rather than printing the value inside the function, it should return the greater number:
def checkgreaternumber(number1,number2):
if number1 > number2:
return number1
else:
return number2
print ("My greater number in 2 and 4 is ",checkgreaternumber(2,4))
print ("My greater number in 3 and 1 is ",checkgreaternumber(3,1))
In this case, as we can see, the function returns the value, and the result is returned on the line where the function was called. In this case, as it was called inside the print function, it evaluates the input and returns the value, which also gets printed out inside the same print function.
#PowerShell sample code
function checkgreaternumber($number1,$number2)
{
if ($number1 -gt $number2)
{
return $number1
}
else
{
return $number2
}
}
write-host ("My greater number in 2 and 4 is ",(checkgreaternumber 2 4))
write-host ("My greater number in 3 and 1 is ",(checkgreaternumber 3 1))
Another important aspect of a function is the default values that we can provide in a function. Sometimes we need to write functions that might take multiple, say 4, 5, or more, values as inputs. Since it becomes hard to know what values we need and in which order for the function, we can ensure that the default value is taken into consideration if any value is not provided when calling that specific function:
def checkgreaternumber(number1,number2=5):
if number1 > number2:
return number1
else:
return number2
print ("Greater value is",checkgreaternumber(3))
print ("Greater value is",checkgreaternumber(6))
print ("Greater value is",checkgreaternumber(1,4))
The output of the code execution is given as:
- As we can see in the preceding output, we specified the default value of number2 as 5. Now, as we can see in the first call to the function, we only give the value 3. Now, as the function needs two inputs or parameters, but we provided only one, the second value for the function is taken from the default one, which is 5 in this case. Hence, a comparison will be done between 3 and 5 to get the greater number.
- In the second call to the function, a similar call is made with 6, and since no other value was provided, the comparison was between 6 and 5, and result returned was the greater value, which is 6.
- In the third call, we provide both values, which overrides any default value, so a comparison was done between 1 and 4. The result was evaluated and the output of 4 was returned.
Another important consideration is the localization of a variable in a function:
globalval=6
def checkglobalvalue():
return globalval
def localvariablevalue():
globalval=8
return globalval
print ("This is global value",checkglobalvalue())
print ("This is global value",globalval)
print ("This is local value",localvariablevalue())
print ("This is global value",globalval)
The output of the preceding code is as follows:
- In the preceding output, we define a variable named as globalval with a value of 6. In the checkglobalvalue function, we just return the value of the globalvalvariable, which prints a value of 6 as we call the first print function.
- The second print function just prints the value of the same variable, which also prints 6.
- Now, in the third print function, localvariablevalue, we call the same globalval, but give it a value of 8 and return the value of globalval. In the print value of local, it prints the result as value 8. It is not assumed that the globalval variable has a value of 8 now. But, as we can see in the last print function, it still prints a value of 6, when we call the print function to print the value of globalval.
This clearly shows that any variable inside a function is locally effective, or is localized, but does not have any impact on any variables outside the function. We need to use the global command to reference the global variable and remove the localization impact of it.
Here is the same example before using the global command:
As can we see in the preceding output, if we change the value of the globalval global variable inside the localvariablevalue function, we see the effect on the global variable with a new value of 8.