Recursivity
GoMelan supports recursivity.
fn factorial(n: Int) -> Int
{
if (n <= 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
fn main() -> Int
{
return factorial(5);
}
Output:
120
Declare the factorial function.
Recursively calculating the factorial of n.
Call the factorial function in main with n=5.
Last updated
Was this helpful?