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:
Method Overloading in Scala
Next article icon

Scala | Repeated Method Parameters

Last Updated : 29 Mar, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

In Scala, Repeated Method parameters are supported, which is helpful when we don’t know the number of arguments a method requires. This property of Scala is utilized in passing limitless parameters to a method defined.
Important points :

  • The methods with Repeated Parameters should have each of the parameters of same type.
  • A Repeated parameter is always the last parameter of the method defined.
  • The method defined by us, can have only one parameter as Repeated Parameters.

Example:




// Scala program of repeated 
// parameters
  
// Creating object
object repeated
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating a method with
        // repeated parameters
        def add(x: Int*)
        : Int =
        {
  
            // Applying 'fold' method to 
            // perform binary operation
            x.fold(0)(_+_)
      
        }
  
        // Displays Addition
        println(add(2, 3, 5, 9, 6, 10, 11, 12))
    }
}
 
 
Output:
  58  

In order to add any number of extra parameters, we need to put * mark after the type of the parameter being used.
Some more examples of Repeated Parameters:

  • An Array can be passed in the Repeated Parameter method.
    Example:




    // Scala program of repeated 
    // parameters
      
    // Creating object
    object arr
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Creating a method with
            // repeated parameters
            def mul(x: Int*)
            : Int =
            {
      
                // Applying 'product' method to 
                // perform multiplication
                x.product
          
            }
      
            // Displays product
            println(mul(Array(7, 3, 2, 10): _*))
        }
    }
     
     
    Output:
      420  

    In order to pass an array in the defined method, we need to put a colon i.e, : and _* mark after passing the values in the array.

  • An example to show that Repeated Parameters are always the last parameter of the method defined.
    Example:




    // Scala program of repeated 
    // parameters
      
    // Creating object
    object str
    {
      
        // Main method
        def main(args: Array[String])
        {
      
            // Creating a method with
            // repeated parameters
            def show(x: String, y: Any*) =
            {
          
                // using 'mkString' method to
                // convert a collection to a
                // string with a separator
                "%s is a %s".format(x, y.mkString("_"))
            }
      
            // Displays string 
            println(show("GeeksforGeeks", "Computer",
                                "Sciecne", "Portal"))
        }
    }
     
     
    Output:
      GeeksforGeeks is a Computer_Sciecne_Portal  

    Here, format is used to format strings.



Next Article
Method Overloading in Scala
author
nidhi1352singh
Improve
Article Tags :
  • Scala
  • Scala
  • Scala-Method

Similar Reads

  • Parameterless Method in Scala
    Prerequisites - Scala | Functions A parameterless method is a function that does not take parameters, defined by the absence of any empty parenthesis. Invocation of a paramaterless function should be done without parenthesis. This enables the change of def to val without any change in the client cod
    2 min read
  • Implicit Parameters In Scala
    Implicit parameters are the parameters that are passed to a function with implicit keyword in Scala, which means the values will be taken from the context in which they are called. In simpler terms, if no value or parameter is passed to a method or function, then the compiler will look for implicit
    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
  • Method Overriding in Scala
    Method Overriding in Scala is identical to the method overriding in Java but in Scala, the overriding features are further elaborated as here, both methods as well as var or val can be overridden. If a subclass has the method name identical to the method name defined in the parent class then it is k
    8 min read
  • Method Overloading in Scala
    Method Overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement function overloading by defining two or more functions in a class sharing the same name. Scala can distinguish the methods with different method sig
    5 min read
  • Scala Mutable SortedSet +() method
    In Scala mutable collections, +() method is utilized to create a new SortedSet with an additional element unless the element is already present. Method Definition: def +(elem: A): SortedSet[A] Return Type: It returns a new SortedSet with an additional element unless the element is already present. E
    1 min read
  • Scala Float compare() method with example
    The compare() method is utilized to returns 0 if the first number is equal to the second number, 1 if the first number is greater than the second number and finally -1 if the first number is less than the second number. Method Definition: (First_Number).compare(Second_Number) Return Type: It returns
    2 min read
  • Scala Mutable SortedSet take() method
    In Scala mutable collections, take() method is utilized to return a SortedSet consisting of first 'n' elements of the SortedSet. Method Definition: def take(n: Int): SortedSet[A] Where 'n' is specifies the number of element to select. Return Type: It returns a SortedSet consisting of first 'n' eleme
    1 min read
  • Scala | Methods to Call Option
    The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null. There are a few methods that
    5 min read
  • How to define Optional Parameter in Scala?
    Optional parameters in Scala provide us with a procedure or a way to define some default values for function parameters. The need to define default values comes when a value is not provided for the parameter during function invocation. These are also useful when we want to provide some default or sp
    5 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