Still under HEAVY construction

My very “personal” explanation about recusion, via code examples with comments

The state (value) of variables is like the very fist invocation. Any update is “local” to that call of the function

If a variable has to be “like” global (so accesible to all function calls), it can passed as a parameter (please, do not make it global!!

SumDigits sums the each single number that conform a number. For intance, 425 is 11.

def sumdigits_iter(number):

  digisum = 0
  for a in range(len(str(number))):
    digisum += int(str(number)[a])

  return digisum
def sumdigits_recur(number):
  return sumdigits_recur_helper(number, 0)

def sumdigits_recur_helper(number, digisum):

  if len(str(number)) < 2:
    # aqui es retorna el valor calculat final (estem a condicio final)
    # cada recursio digisum s'ha anat increment amb la suma.
    # si posessim un valor 'a pel' ens retornaria el valor (p.e. 'return 44')
    return (digisum + number)
    digisum += int(str(number)[:1])

    # Aumentada la variable de pas (digisum), la pasem a la seguent recusio
    # amb l'objectiu que arrivi a la operacio base havent sumat cada digit
    # Cada "retornada" arrosega el valor final de l'operacio base (sempre el mateix).
    # Es a dir, a les anades,  pasem el numero sense l'ultim digit, que anem sumant a digisum
    # A les tornades (retorns) s'arrosega el valor de la suma final a la operacio base

    return sumdigits_recur_helper(int(str(number)[1:]), digisum)
# divide by 10 gives the all but last digit. modulus of 10 gives a last digit...
# en aquest cas, a les anades anem pasant el numero sense l'ultim digit i en "guardem" el digit
# a les tornades, en pasen un numero (que es la suma del digits previs) i el retornem cap a dalt
# sumant el digit guardat.
# check good explanation of recursivity at

def sumdigits_recur_nicer(number):
  if (number) < 10:
    return number
    casi_tot, ultim = number//10, number%10

    return sumdigits_nicer_recur(casi_tot) + ultim
    # equivalent a
    #  retornat = sumdigits_nicer_recur(casi_tot)
    #  return retornat + ultim