Scala 3-Method-2
Defining Methods
Scala 3 methods may contains many types of parameters:
Generic (type) parameters
Default parameter
Multiple parameter
Context-provided parameters
By-name parameters
Method can be defined either with return or without return:-
def mulMethod(a: Int, b: Int): Int = a + b//method with return type
def mulMethod(a: Int, b: Int) = a + b//method without return type
The keyword def is used to define a method
The Scala standard is to name methods using the camel case convention
Method parameters are always defined with their type
Declaring the method return type is optional
Methods can be multi lines, or just one line
Here are two examples of a one-line method named add that takes two Int input parameters. The first method shows the method’s Int return type, and the second does not:
It is recommended to annotate publicly visible methods with their return type. Declaring the return type can make it easy, and readable.
Calling methods
Invoking a method is simple:
val p = mulMethod(5,4)
or
val q = mulMethod(5,4)