Functions are often useful for calculating mathematical equations
Many mathematical functions are expressed recursively. Recursive functions are a natural programming mechanism in such cases.
Problem: multiply the numbers from 1 to N
N! ("N factorial") = 1 x 2 x 3 x ... x N
fact(n) = | n * fact (n-1) | {n>1} |
1 | {n=1} |
Code
function factorial (n: in integer) return integer is begin if (n=1) then return 1; end if; return n * factorial(n-1); end;