Lesson 7 of 15

Recursion

Recursion

F# supports recursion with let rec:

let rec factorial n =
    if n <= 1 then 1
    else n * factorial (n - 1)

printfn $"{factorial 5}"  // 120

The rec keyword is required — F# doesn't allow a function to call itself without it.

Your Task

Write a recursive function power that takes a base b and exponent e (both integers) and returns b raised to the power e. Assume e >= 0. Base case: power b 0 = 1.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.