Scala Functions - Scala

What is Scala - Functions?

A function is a group of statements that are written to perform a task. You can divide up your code into various separate functions. How you divide up your code among different functions depends upon you, but logically, the division usually is such that each function performs a specific task.

Scala has both functions and methods and the usage of the terms method and function happens interchangeably with a minor difference. A Scala method is generally a part of a class which has its name, a signature, optionally some annotations, and some bytecode but where as a function in Scala is a complete object which can be assigned to any given variable. In other words, a function, which is defined as a member of some object, is called a method.

A function definition can appear anywhere in a source file and Scala permits nested function definitions, that is, function definitions inside other function definitions. Some of the most important points to note is that the Scala function's name can have characters like +, ++, ~, &,-, --, \, /, :, etc.

Function Declarations

A Scala function declaration has the following form -

Methods are implicitly declared 'abstract' if you don’t use the equals sign and the method body.

Function Definitions

A Scala function definition has the following form -

Syntax

Here,the return type could be any valid Scala data type and the list of parameters will be a list of variables separated by the comma and list of parameters and return type are optional. Very similar to Java, a return statement can also be used along with an expression in case the function returns a value. Following is the function which will add two integers and return their sum ?

Syntax

A function, which does not return anything can return a Unit that is equivalent to void in Java and indicates that the function does not return anything. The functions which do not return anything in Scala, they are called procedures.

Syntax

Here is the syntax -

Calling Functions

Scala provides a number of syntactic variations for invoking the methods. Following is the standard way to call a method ?

If a function is being called using an instance of the object, then we would use the dot notation similar to Java as follows ?

Try the below example program to define and then call the same function.

Example

Save the above program in Demo.scala. The following commands can be used to compile and execute this program.

Command

Output

Scala functions are the heart of Scala programming and that's why Scala is assumed as a functional programming language. Following are some important concepts related to Scala functions which should be understood by a Scala programmer.

Functions Call-by-Name

Functions with Named Arguments

Function with Variable Arguments

Recursion Functions

Default Parameter Values

Higher-Order Functions

Nested Functions

Anonymous Functions

Partially Applied Functions

Currying Functions

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Scala Topics