Hi, I'm Ataias Reis

Programmer, made in Brazil, Alma mater University of Brasilia

30 days of code in Go: Day 9 - Recursion

Hi there! Here I am coding again. For those of you readers who may not know what recursion is, I have a brief explanation: a function is called a recursive function when it calls itself. A very nice example is the factorial, which is also what I am going to code today. For the factorial of $N$, $f(N)$, we can state that $f(N) = N\times f(N-1)$. What we see is that the function is represented by itself, but with other argument and it can keep this going up to a certain point for which we know the answer like $f(1)=f(0)=1$. Given this introduction, I will code.

package main

import "fmt"

// N > 0 always!
func factorial(N uint) uint {
  if N > 1 {
   return N * factorial(N-1)
  return 1

func main() {

  var N uint
  fmt.Printf("%d\n", factorial(N))


Usually I say what point I had difficulties, but I confess I had no problems with this one today. Thanks for reading! If you have any doubts, critics or suggestions, let me know!

comments powered by Disqus