Scala–Methods vs Functions

An excellent explanation on Scala Methods and Functions:

http://jim-mcbeath.blogspot.in/2009/05/scala-functions-vs-methods.html

Apparently, Methods in Scala are different from Functions. From what I understand, a method is similar to a method in Java – it takes input and produces an output. But a Function is an object. Also takes an input and produces an output, but is an object. Here’s how they’re different:

def f1(x:Int):Int = x*2     //Method that doubles input

def f2 = (x:Int)=>x*2      //Function that doubles input

//f1.toString                    //Cannot be done!

f2.toString                      //Is perfectly valid

For more details, I’d suggest you take a look at Jim McBeath’s explanation.

Advertisements