Scala | Nested Functions Last Updated : 06 Jun, 2021 Comments Improve Suggest changes Like Article 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 Scala // 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 // 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 . Comment More infoAdvertise with us Next Article Scala | Nested Functions 29AjayKumar Follow Improve Article Tags : Scala Scala-Method Similar Reads Pure Function In Scala In any programming language, there are two types of functions: 1. PURE FUNCTIONS 2. IMPURE FUNCTIONSThere is a basic difference between the two, that is pure function doesnât change the variable itâs passed and an impure function does. For example, there exists a function which increases the input b 3 min read Partial Functions in Scala Introduction: When a function is not able to produce a return for every single variable input data given to it then that function is termed as Partial function. It can determine an output for a subset of some practicable inputs only. It can only be applied partially to the stated inputs. Some import 4 min read Scala | Functions Call-by-Name In Scala when arguments pass through call-by-value function it compute the passed-in expression's or arguments value once before calling the function . But a call-by-Name function in Scala calls the expression and recompute the passed-in expression's value every time it get accessed inside the funct 3 min read Scala | Functions - Basics A function is a collection of statements that perform a certain task. One can divide up the code into separate functions, keeping in mind that each function must perform a specific task. Functions are used to put some common and repeated task into a single function, so instead of writing the same co 3 min read Anonymous Functions in Scala In Scala, An anonymous function is also known as a function literal. A function which does not contain a name is known as an anonymous function. An anonymous function provides a lightweight function definition. It is useful when we want to create an inline function. Syntax: (z:Int, y:Int)=> z*yOr 2 min read Like