Scala Methods and Functions | Tutorial
Tutorial --
Scala Tutorial
Scala TutorialScala IntroductionScala Installation and Environment ConfigurationScala Basic SyntaxScala Data TypesScala Literals Scala Escape Characters Scala VariablesScala Access ModifiersScala OperatorsScala IF...ELSE StatementsScala LoopsScala Methods and FunctionsScala ClosuresScala StringsScala ArraysScala CollectionScala IteratorScala Classes and ObjectsScala TraitScala Pattern MatchingScala Regular ExpressionsScala Exception HandlingScala ExtractorScala File I/O
Scala Loops
Scala Closures
Deep Dive
Programming
Software
Programming Languages
Development Tools
Computer Science
Scripts
Web Services
Web Service
Web Design and Development
Scripting Languages
Scala Methods and Functions
Scala has methods and functions, and the semantic difference between the two is very small. A Scala method is part of a class, while a function is an object that can be assigned to a variable. In other words, a function defined in a class is a method.
Methods in Scala are similar to those in Java; they are part of a class.
Functions in Scala are complete objects; a function in Scala is actually an object of a class that inherits from a Trait.
In Scala, you can define a function using the val statement, and a method using the def statement.
class Test{
def m(x: Int) = x + 3
val f = (x: Int) => x + 3
}
Note: In some translations, there is no distinction between function and method
class Test{
def m(x: Int) = x + 3
val f = (x: Int) => x + 3
}
YouTip