What's the reason of marking a recursive function as rec in F#?

Viewed 2129

I am not sure if this is a stupid question but I was going through the tutorial that comes with VS 2010 and there is a function like this:

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

What's the reason of this recursive function to be marked with the rec keyword?

Is it so that the compiler is assured of it being recursive so can do certain optimizations?

What happens if you exclude it?

5 Answers

What's the reason of this recursive function to be marked with the rec keyword?

To tell the compiler that any uses of the function name inside the body of the function refer to it recursively rather than to a previously-defined value of the same name.

Is it so that the compiler is assured of it being recursive so can do certain optimizations?

No.

What happens if you exclude it?

You lose the ability for the function you are defining to refer to itself in its function body and gain the ability to refer to previously-defined values of the same name.

Related