I'm learning Programming Paradigms in my University and reading this course material provided by the lecturer that defined a function this way:
val double = (x: Int) => 2 * x
double: Int => Int = <function1>
But from my own studies I found and got used to defining the same function like this:
def d (x: Int) = 2 * x
d: (x: Int)Int
I'm new to Scala. And both definitions give a result of:
res21: Int = 8
Upon passing 4
as the parameter.
Now my main question is why would the lecturer prefer to use val
to define a function? I see it as longer and not really necessary unless using val
gives some added advantages that I don't know of. Besides I understand using val
makes some name a placeholder so later in the program, I could mistakenly write val double = 5
and the function would be gone!
At this stage I'm quite convinced I learned a better way of defining a function unless someone would tell me otherwise.