# Recursive Power Function

Page 1 of 1

## 4 Replies - 3509 Views - Last Post: 08 January 2015 - 06:46 PM

### #1 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

# Recursive Power Function

Posted 01 March 2012 - 08:12 PM

Description: Finds r^p recursively
```double power(int r, int p) {
if(p == 0) {
return 1.0;
}
return r * power(r, p - 1);
}

```

Is This A Good Question/Topic? 0

## Replies To: Recursive Power Function

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12616
• Posts: 45,770
• Joined: 27-December 08

## Re: Recursive Power Function

Posted 09 March 2012 - 05:21 PM

Approved, but to note, the precondition is that p >= 0.

### #3 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Recursive Power Function

Posted 10 March 2012 - 01:00 PM

Um. no it's not. I suppose p <= 0 would also work

### #4 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Recursive Power Function

Posted 10 March 2012 - 01:02 PM

I am trying to write "I supposed p less than or equal to 0 would also work"

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12616
• Posts: 45,770
• Joined: 27-December 08

## Re: Recursive Power Function

Posted 08 January 2015 - 06:46 PM

If p < 0, this will recurse infinitely. So p >= 0 must hold for this code to terminate.