# Scala | Nested Functions

A function definition inside an another function is known as Nested Function. It is not supported by C++, Java, etc. In other languages, we can call a function inside a function, but it’s not a nested function. In Scala, we can define functions inside a function and functions defined inside other functions are called nested or local functions.
Syntax:

def FunctionName1( perameter1, peramete2, ..) = {
def FunctionName2() = {
// code
}
}

Single Nested Function
Here is an example of single nested function that takes two numbers as parameters and returns the Maximum and Minimum of them.
Example

 `// Scala program of Single Nested Function ` `object` `MaxAndMin  ` `{ ` `    ``// Main method ` `    ``def` `main(args``:` `Array[String])  ` `    ``{ ` `        ``println(``"Min and Max from 5, 7"``) ` `                ``maxAndMin(``5``, ``7``); ` `    ``} ` `     `  `    ``// Function ` `    ``def` `maxAndMin(a``:` `Int, b``:` `Int) ``=` `{ ` `     `  `       ``// Nested Function ` `       ``def` `maxValue() ``=` `{  ` `          ``if``(a ``>` `b)  ` `          ``{ ` `              ``println(``"Max is: "` `+ a) ` `          ``} ` `          ``else` `          ``{ ` `              ``println(``"Max is: "` `+ b) ` `          ``} ` `       ``} ` `     `  `       ``// Nested Function ` `       ``def` `minValue() ``=` `{ ` `          ``if` `(a ``<` `b) ` `          ``{ ` `              ``println(``"Min is: "` `+ a) ` `          ``} ` `          ``else` `          ``{ ` `              ``println(``"Min is: "` `+ b) ` `          ``} ` `       ``} ` `       ``maxValue(); ` `       ``minValue(); ` `    ``} ` `} `

Output

```Min and Max from 5, 7
Max is: 7
Min is: 5```

In above code maxAndMin is a function and maxValue is another inner function returns maximum value between a and b similarly minValue is another inner function which is also nested function this function returns minimum value between a and b.
Multiple Nested Function
Here is an implementation of multiple nested function.
Example

 `// Scala program of Multiple Nested Function ` `object` `MaxAndMin  ` `{ ` `    ``// Main method ` `    ``def` `main(args``:` `Array[String])  ` `    ``{ ` `        ``fun(); ` `    ``} ` `     `  `    ``// Function  ` `    ``def` `fun() ``=` `{ ` `         `  `        ``geeks(); ` `         `  `        ``// First Nested Function  ` `        ``def` `geeks() ``=` `{ ` `            ``println(``"geeks"``); ` `             `  `            ``gfg(); ` `             `  `            ``// Second Nested Function  ` `            ``def` `gfg() ``=` `{ ` `                ``println(``"gfg"``); ` ` `  `                ``geeksforgeeks(); ` ` `  `                ``// Third Nested Function  ` `                ``def` `geeksforgeeks() ``=` `{ ` `                    ``println(``"geeksforgeeks"``); ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` `} `

Output

```geeks
gfg
geeksforgeeks```

In above code fun is a function and geeks, gfg, geeksforgeeks are nested functions or local function .

My Personal Notes arrow_drop_up Just another competitive programmer and hard worker

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Akj

Article Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.