Skip to content
Exercise · Functions · lesson 10 of 10

Exercise: power by recursion

Write a recursive function func power(base, exp int) int that returns base raised to the power exp.

  • power(2, 10) → 1024
  • power(-2, 3) → -8
  • power(7, 0) → 1

Constraints: exp is from 0 to 18, base is from -10 to 10 and may be negative or zero; any number to the power 0 is 1, including power(0, 0), and the result always fits in an int.

Do not add extra imports unless the task says so explicitly.

power.go — go
go · 6 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way