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 Parameters a and b before being called into main. Its purpose is to only compute the expression a+b
  • In main Function, the Parameters 3 and 4 are passed. This computation is stored in the Immutable Variable result and the Value of 7 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.