July 2017
Intermediate to advanced
796 pages
18h 55m
English
Before learning how a Scala method returns a value, let's recap the structure of a method in Scala:
def functionName ([list of parameters]) : [return type] = { function body value_to_return}
For the preceding syntax, the return type could be any valid Scala data type and a list of parameters will be a list of variables separated by a comma and a list of parameters and return type is optional. Now, let's define a method that adds two positive integers and returns the result, which is also an integer value:
scala> def addInt( x:Int, y:Int ) : Int = { | var sum:Int = 0 | sum = x + y | sum | }addInt: (x: Int, y: Int)Intscala> addInt(20, 34)res3: Int = 54scala>
If you now call the preceding method from the main() method with ...
Read now
Unlock full access