Open In App

Scala | Nested Functions

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
5 Likes
Like
Report

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( parameter1, parameter2, ..) = { 
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 
 

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 
 

Output 
 

geeks
gfg
geeksforgeeks


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


Article Tags :

Similar Reads