Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Scala | reduce() Function
Next article icon

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 .
 



Next Article
Scala | reduce() Function
author
29AjayKumar
Improve
Article Tags :
  • Scala
  • Scala-Method

Similar Reads

  • Scala | reduce() Function
    The reduce() method is a higher-order function that takes all the elements in a collection (Array, List, etc) and combines them using a binary operation to produce a single value. It is necessary to make sure that operations are commutative and associative. Anonymous functions are passed as paramete
    3 min read
  • 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 | Method Invocation
    Method Invocation is a technique that demonstrates different syntax in which we dynamically call methods of a class with an object. The naming conventions of Scala are same as of Java, that are:- There should not be any space between the invocation object/target and the dot(.) nor a space between th
    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
  • Scala | Named Arguments
    In Scala when arguments passes through the function with a named parameters, we can label the arguments with their parameter names.These named arguments are cross matched with the named parameters of function. In normal scenario Unnamed Parameters uses Parameter Positions to make Function or Constru
    3 min read
  • Scala Context Bound
    Context bound is a shorthand for expressing the common pattern of a context parameter that depends on a type parameter. Type parameters are placeholders for types, these are used to write type generic code, and these are declared in []. Context parameters are implicit type parameters, they help enri
    4 min read
  • Scala | Partially Applied functions
    The Partially applied functions are the functions which are not applied on all the arguments defined by the stated function i.e, while invoking a function, we can supply some of the arguments and the left arguments are supplied when required. we call a function we can pass less arguments in it and w
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences