Scala – Nested Function

In Scala, a function defined inside another function is called nested function or local function. This feature is not supported by Java. In other languages, we can call a function from another function but that is not nested function. Multiple function definitions can be defined inside parent function.Let’s understand this with examples.


A function named calculation has been written and inside that multiple nested functions have been created as shown below. These nested function will do different calculations and print the result.

A nested function can be defined inside another function and so on. It is called nesting of function. Theoretically, nesting is possible to unlimited depth but practically it id done to few levels.Let’s see an example below where nesting is done till 3rd level.



In the below example, function is doing factorial of given number. A nested function named fact is defined inside parent function named factorial.

Scala - Recursive Functions
Scala - Pass function as a parameter
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *