-4

I am new to scala. I have worked on java. defining of function in scala by using = operator does not make sense for me.

def abc(a:Int,b:Int):Int = 
    a+b

can anybody explain? in the above code, why i had to use = operator.

irfan khan
  • 35
  • 3
  • See [this question](http://stackoverflow.com/questions/1661817/scala-def-foo-1-vs-def-foo-1) for more information about the difference of using or not using = in method declaration. – diiN__________ Dec 14 '16 at 06:45

3 Answers3

7

I believe there are many reasons. Just some of them:

1) uniform access:

val abc: Int = 5 //value
def abc: Int = 5 //factory

Note that such def abc can be implemented with val abc inside successor:

trait Interface {
  def abc: Int //factory (reader)
}

object Implementation extends Interface {
  val abc: Int = 5 //singleton
}

Some advanced examples: Why it's impossible to override `var` with `def` in Scala?

2) functions are just named lambda's:

val abc = (a: Int, b: Int) => a + b //lambda assigned to abc value
def abc (a:Int, b:Int): Int = a + b //abc method, that could be converted to lambda/function with eta-expansion: "abc _"

The syntactic difference between first and second one is just position of braces, so they are quite similar.

3) that function actually returns value, you might think of the a + b block independently from function definition, so then:

def abc ...: Int = {
   val z = a + b
   z
}

is almost like

val abc: Int = {
  val z = a + b
  z
}

But a and b are unknown, so we need to describe them too:

def abc(a: Int, b: Int): Int = {
  val z = a + b
  z
}

Otherwise, if your method doesn't return anything, you can omit = sign:

def abc(a: Int, b: Int) {
  println(a + b)
}

Which is equivalent to:

def abc(a: Int, b: Int): Unit = {
  println(a + b)
}

Where Unit (inhabited type with only one possible value, so it contains no information) roughly means that no value was returned.

See also official doc and language spec

Community
  • 1
  • 1
dk14
  • 22,206
  • 4
  • 51
  • 88
3

This approach enhances the functional aspect of programming, where functions are no different than other data types.

e.g. In Scala

val number = 10
val function = (a:Int, b:Int) => a + b

Here, both the values number and function are immutable variables. Only difference is their types. number is of type Int, while function is of type (Int, Int) => Int

This enables functions to be treated just like Data and be passed around like other variables. This enhances into concepts like Higher Order Functions which can accept and return function values as opposed to literal values. As @dk14 mentioned, it's about uniform access.

Sid
  • 4,893
  • 14
  • 55
  • 110
1

There is one reason I know why scala uses = in method declaration is:

To return any value from method definition ,= has to be used or it cant return any value even if there is a return statement in method.

For Eg: look at below two code snippets:

def method1()
{
  val a=1
  return a
}

def method2()=
{
  val a=1
  return a
}

method1 return type is always unit even if it returns some values whereas method2 returns value a .

A srinivas
  • 791
  • 2
  • 9
  • 25
  • 1
    The last sentence here is wrong. method1 returns a unit, method2 returns a.type – Tim Dec 14 '16 at 12:16