rock
rock

Reputation: 107

How to create a loop to feed back the result as new param of the function? (PowerShell)

My current script is:

function F
{
     param ([int]$OF)
     $OF / 2
}

$OF = 100

if ((F $OF) -ge 2)
{
     F (F $OF)
}

The result is 25 now. But what I want to do is to add a loop to this script. Within the loop, the result F (F $OF) will be fed back as the new $OF of the function. Then the function runs again. It will not stop feeding back the result until the result is -lt 2. The whole process is more like the optimization: keep reducing the value until it meets the objective. However, I am not sure how to create such a loop. Use FOR LOOP? Or DO UNTIL LOOP?

Upvotes: 1

Views: 76

Answers (2)

The original description in your question seems to point to a recursive method as a recursive function can run until a condition is met (in the same manner as a loop). In this case a simple while loop or do/while loop would probably work but this also provides an excellent opportunity to learn about recursion for later problems or assignments (such as flattening arrays or sorting/searching problems).

Simple Recursive Method

A note on the code: it is good coding practice to start learning naming standards. In this case, variables created within functions shouldn't have the same name as other functions (unless a $global is needed) and one letter functions are rarely a good idea - both of these can cause confusion and make code hard to parse later.

function CalculateF #changed from F
{
     param ([int] $cur_of) #changed from $OF so no confusion with calling variable

     $cur_of

     if ($cur_of -gt 2)
     {
        CalculateF -cur_of ($cur_of/2) # calls itself with halved value
     }
}

$OF = 100

if (CalculateF -cur_of $OF -ge 2) #changed these function calls to call the parameter directly
{
     CalculateF -cur_of $OF
}

This function runs, prints the value of the current OF (cur_of), checks to see if the value of cur_of is greater than 2. Than CalculateF calls itself (that is simple recursion) with cur_of/2. Or if it is not it completes the function.

For Loop Version

As @Kiran already put a version with a do/while loop and function - I'll put the for loop version which does not require or make sense with a function at all. As a for loop can perform simple mathematical calculations on the iterator not just +/- 1, as such:

$val = 100

for ($i=$val; $i -ge 2; $i /= 2)
{
  # Sets iterator to value and loops through (dividing by 2 each time)
  [int] $i
}

Note: Tested above with powershell -version 2 ./scriptname.ps1 to ensure no versioning issues

Upvotes: 1

Kiran Reddy
Kiran Reddy

Reputation: 2904

couple of ways to do this:

function F
{
 param ([int]$OF)
 do 
 {
    $result = $of / 2
    $of = $result
    $result
 }
 while( $result -gt 2)

}



f -OF 200

-OR

function F2
{
   param ([int]$OF)

   $of / 2
}

#Declare the parameter value
$val = 100


do
{
  $result = F2 -OF $val
  $val = $result
  $result
}
while($result -gt 2)

Upvotes: 2

Related Questions