Scala Arguments - chrisbitm/python GitHub Wiki
Positional Arguments (Or Parameters) refer to the method of passing Arguments to a Function. Each Argument is matched to a corresponding Parameter in the order they appear in your code.
Method 1 (Positional)
def add(a: Byte, b: Byte): Byte = {
return (a + b).toByte
}
def main(args: Array[String]): Unit = {
val result: Byte = add(3, 4);
print(result)
}
- The
add
Function passes the required Parametersa
andb
before being called intomain
. Its purpose is to only compute the expressiona+b
- In
main
Function, the Parameters3
and4
are passed. This computation is stored in the Immutable Variableresult
and the Value of7
is printed to Console.
In the above example, a = 3
and b = 4
. If you were to reverse the order it wouldn't matter in this Use-Case because the sum of two numbers is not going to change no matter the position. Despite that, keep in mind the order for which you pass your Arguments when using the above method.
Method 2 (Named)
def add(a: Byte, b: Byte): Byte = {
return (a + b).toByte
}
def main(args: Array[String]): Unit = {
val result: Byte = add(b = 4, a = 3);
print(result)
}
- Using this method in the example above, the Arguments are named so there is no need to be cautious of Argument placement.