# Power

## Recommended Posts

This is the code which I wrote with For loop to caculate Exponent..

It can be listed pow 1, pow2...,etc

Noraml Forloop ver.

```\$x = InputBox("Power", "Base:")
\$y = InputBox("Power", "Exponent:")
MsgBox(0, "", powFor(\$x, \$y))
Func powFor(\$x, \$y)
Dim \$n, \$t = \$x, \$c = 1, \$p = 0
If \$y == 0 Then
Return 1
ElseIf \$y < 0 Then
For \$i = StringRegExpReplace(\$y, '-(\d+)', '\$1') To 1 Step - 1
If \$c = 1 Then
\$x *= 1
\$c += 1
\$p += 1
\$n &= "p" & \$p & " : " & (1 / \$x) & @CRLF
Else
\$x *= \$t
\$p += 1
\$n &= "p" & \$p & " : " & (1 / \$x) & @CRLF
EndIf
Next
Else
For \$i = \$y To 1 Step - 1
If \$c == 1 Then
\$x *= 1
\$c += 1
\$p += 1
\$n &= "p" & \$p & " : " & \$x & @CRLF
Else
\$x *= \$t
\$p += 1
\$n &= "p" & \$p & " : " & \$x & @CRLF
EndIf
Next
EndIf
Return \$n
EndFunc```

And this is the Recursion for caculating Exponent..

But how can I list each pow in Recursion ver. as For loop ver.

Recursion ver.

```MsgBox(0, "", powRec(\$x, \$y))
Func powRec(\$x, \$y)
If \$y == 0 Then
Return 1
ElseIf \$y == 1 Then
Return \$x
Else
Return \$x * powRec(\$x, \$y - 1)
EndIf
EndFunc```
Edited by Underdogger

##### Share on other sites

the simplest way - although probably will not be acceptable by your professor - is to dedicate a global var to accumulate the steps.

declare global var \$s

the func returns the computed value, not the string.

the func calculates it into \$r before return, because \$r is processed before return

if you want the func to return the string, without using any global var, then you will need to pass it as a 3rd parameter and perform some string manipulation.

PS how about some validation routines? your func fails with negative exponent!

```Global \$s=''
\$x = InputBox("Power", "Base:")
\$y = InputBox("Power", "Exponent:")
MsgBox(0, 'pow', powRecS(\$x, \$y))
MsgBox(0, '\$s', \$s)

Func powRecS(\$x, \$y)
Local \$r
If \$y == 0 Then
\$r=1
ElseIf \$y == 1 Then
\$r=\$x
Else
\$r=\$x * powRecS(\$x, \$y - 1)
EndIf
\$s=\$s&' p'&\$y&' : '&\$r&@CR
Return \$r
EndFunc```

##### Share on other sites

the simplest way - although probably will not be acceptable by your professor - is to dedicate a global var to accumulate the steps.

declare global var \$s

the func returns the computed value, not the string.

the func calculates it into \$r before return, because \$r is processed before return

if you want the func to return the string, without using any global var, then you will need to pass it as a 3rd parameter and perform some string manipulation.

PS how about some validation routines? your func fails with negative exponent!

```Global \$s=''
\$x = InputBox("Power", "Base:")
\$y = InputBox("Power", "Exponent:")
MsgBox(0, 'pow', powRecS(\$x, \$y))
MsgBox(0, '\$s', \$s)

Func powRecS(\$x, \$y)
Local \$r
If \$y == 0 Then
\$r=1
ElseIf \$y == 1 Then
\$r=\$x
Else
\$r=\$x * powRecS(\$x, \$y - 1)
EndIf
\$s=\$s&' p'&\$y&' : '&\$r&@CR
Return \$r
EndFunc```

Is it possible to set negative Exponent in Recusion version?

## Create an account

Register a new account