Skip to content

Latest commit

 

History

History
39 lines (30 loc) · 871 Bytes

recursive-functions.md

File metadata and controls

39 lines (30 loc) · 871 Bytes
Topic Recursive Functions
Git sample FirstClassFuncTest.scala
References fruzenshtein.com

  • A recursive function is a function which calls itself. Example of a non recursive function,
def standardSum(a: Int, b: Int): Int = {

    if (a > b) 0
    else {
        var result = 0
        for (number <- a to b)
            result += number
        result
    }
}

/* Call to standard sum method */ 
standardSum(0, 10)
  • Example of a recursive function performing same task,
def recursiveSum(a: Int, b: Int): Int = {

    if (a > b) 0
    else {
        a + recursiveSum(a+1, b)
    }
}

/* Call to recursive sum method */ 
recursiveSum(0, 10)