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 Constructor calls, but These named parameters allows us to change order of the arguments passes to a function by simply exchanging the order.
Syntax:
Function Definition : def createArray(length:int, capacity:int); Function calling : createArray(capacity=20, length:10);
Precautions –
-> If some arguments are named and others are not, the unnamed arguments must come first
function(0, b = "1")-> Order Interchange is valid
function(b = "1", a = 0)-> Not accepted , error: positional after named argument
function(b = "1", 0)
-> Not accepted , parameter ‘a’ specified twice as ‘0’ in first position and again as a = 1
function(0, a = 1)
Note : If x argument expression has the form x = expr and x is not a parameter name of the method, the argument is treated as an assignment expression to some variable x.
Example :
// Scala program using Named arguments // Creating object object GFG { // Main method def main(args: Array[String]) { // passed with named arguments printIntiger(X = 6, Y = 8); } // Defining a method def printIntiger( X:Int, Y:Int ) = { println("Value of X : " + X ); println("Value of Y : " + Y ); } } |
Value of X : 6 Value of Y : 8
Here, in above example we created the printIntiger function and then we called the function. we use the function parameter names when calling the function. we passed arguments X = 6 , Y = 8 here X and Y are the name of parameters.
Example :
// Scala program using Named arguments // Creating object object GFG { // Main method def main(args: Array[String]) { // without named arguments printName("geeks","for","geeks"); // passed arguments according to order printName(first = "Geeks", middle="for", last = "Geeks"); // passed arguments with different order printName(last = "Geeks", first = "Geeks", middle="for"); } // Defining function def printName( first: String, middle: String, last: String ) = { println("Ist part of name: " + first ) println("IInd part of name: " + middle ) println("IIIrd part of name: " + last ) } } |
Ist part of name: geeks IInd part of name: for IIIrd part of name: geeks Ist part of name: Geeks IInd part of name: for IIIrd part of name: Geeks Ist part of name: Geeks IInd part of name: for IIIrd part of name: Geeks
As we can see in above example, we created the printName function, and then we called the function. Here, we use the function parameter names when calling the function. By changing the order of the arguments like printName(last = “Geeks”, first = “Geeks”, middle=”for”) we can get the same result.
Recommended Posts:
- Golang Program that Uses Named Return Values and Defaults
- Named List in R Programming
- How to create optional arguments for a SASS mixin?
- How to describe “object” arguments in jsdoc?
- How to convert arguments object into an array in JavaScript ?
- Difference between multiple arguments and options object
- Golang program that uses func with two arguments
- Command Line Arguments in Golang
- Function Arguments in R Programming
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Program to print Java Set of characters in Scala
- Scala Map size() method with example
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.

