Scala | Functions – Basics

A function is a collection of statements that perform a certain task. One can divide up the code into separate functions, keeping in mind that each function must perform a specific task. Functions are used to put some common and repeated task into a single function, so instead of writing the same code again and again for different inputs, we can simply call the function. Scala is assumed as functional programming language so these play an important role. It makes easier to debug and modify the code. Scala functions are first class values.

Difference between Scala Functions & Methods: Function is a object which can be stored in a variable. But a method always belongs to a class which has a name, signature bytecode etc. Basically, you can say a method is a function which is a member of some object.

Function Declaration & Definition

In general, function declaration & definition have 6 components:

  • def keyword: “def” keyword is used to declare a function in Scala.
  • function_name: It should be valid name in lower camel case. Function name in Scala can have characters like +, ~, &, –, ++, \, / etc.
  • parameter_list: In Scala, comma-separated list of the input parameters are defined, preceded with their data type, within the enclosed parenthesis.
  • return_type: User must mention return type of parameters while defining function and return type of a function is optional. If you don’t specify any return type of a function, default return type is Unit which is equivalent to void in Java.
  • = : In Scala, a user can create function with or without = (equal) operator. If the user uses it, the function will return the desired value. If he doesn’t use it, the function will not return any value and will work like a subroutine.
  • Method body: Method body is enclosed between braces { }. The code you need to be executed to perform your intended operations.

Syntax:

def function_name ([parameter_list]) : [return_type] = {
   
  // function body

}

Note: If the user will not use the equals sign and body then implicitly method is declared abstract.

Function Calling

There are mainly two ways to call the function in Scala. First way is the standard way as follows:

function_name(paramter_list)

In the Second way, a user can also call the function with the help of the instance and dot notation as follows:

[instance].function_name(paramter_list)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

object GeeksforGeeks {
      
   def main(args: Array[String]) {
         
      // Calling the function
      println("Sum is: " + functionToAdd(5,3));
   }
     
     
   // declaration and definition of function
   def functionToAdd(a:Int, b:Int) : Int = 
   {
         
       var sum:Int = 0
       sum = a + b
  
       // returning the value of sum
       return sum
   }
}

chevron_right


Output:

Sum is: 8


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.