Updated April 15, 2023
Definition of Scala Function
As the name suggests, Scala Function means a piece of code supposed to do a function. Like any other programming language scala, Function works in the same way. The function is nothing but a way to writing our logic in a separate part, or we can say a function is a group of statements that are responsible for performing some specific task. The function can be used where we have the same logic or repetitive code, so instead of writing the code again and again, we can create one function and call that from everywhere. Scala function is also responsible for performing a specific task.
How to Define Functions in Scala?
While defining a function, we need to pay closer attention to the function signature because it is very much important. Our function will totally depend on its signature how we define it. We should keep in mind that what we want from our function and what parameters we are going to provide. Let’s see below;
defname_of_function ([parametre_list]) : [return_type] = {
// function logic
}
Scala Function contained 6 parts while defining; let’s discuss them one by one.
- def: It is a keyword that is available in scala. If you want to define any function, we have to use this keyword at the beginning.
- name_of_function: This is the user-defined name of the function. It should be similar to the logic or task that the function is going to execute while calling. Also, it should be in a camel-case (lower).
- return_type: return type means what we are expecting from the function in return after executing. It can be anything, but it is optional. In java, the default return type is void, and in scale, it is Unit if we do not specify.
- parametre_list: This stands for what we are providing to our function while calling. We have to specify the data type of the parameters as well while declaring inside the square brackets []. We will see them into the practice syntax below.
- =: This can be used with the return type component. It specifies, if the = is there, it means our function is going to return some value. If not, then no value we want. It is like a default return type function.
- function logic: Inside this, we write the whole logic that we want to perform on the calling of function. We can also call the different functions inside this function. Remember body should be enclosed with the {} curly braces.
defcalculateSum ([a:Int, b: Int]) : Int = {
return a + b;
}
This way, we can define it.
How does Function work in Scala?
The function is used to perform the task. To use any function, we need to call it. Scala provides us with different ways to call a function, i.e. we can call them directly or by using the class instance.
[instance].name_of_function
or
function(list_parameter)
In scala, we have two types of functions like any other programming language.
- Parameterized functions: In this type of function, we pass the list of parameters.
- Non-Parameterized functions: In this type of function, we do not pass any parameters to function. That will be empty. Also, we can pass any user-defined value as a parameter also.
Let’s take one example to understand its working;
object Main extends App{
// Your code here!
calculateSum(10, 20)
defcalculateSum(a : Int, b : Int){
var result = a + b ;
println("Result is :: "+ result)
}
}
Above we have defined one function name calculateSum, and it is taking two variables, a and b. Both are of the Integer type. Inside the function body, we have written the logic that we want to perform. We are adding these two values, a and b, holding the value into the third variable named result. After that, we are just printing the value that we obtained. But now we have to call this function, so in the above line, we are calling the function b its name and parameter specified. The number of Parameters we passed and the number defined in the function signature should be the same; otherwise, it will give a compile-time error.
Examples of Scala Function
Examples of (simple function, parameterized function, etc.).
Example #1
This example shows the use of functions without parameters.
Code:
object Main extends App{
// Your code here!
// calling function
simpleFunction()
defsimpleFunction(){
println("This is simple function")
println( "without parameter. ")
}
}
Output:
Example #2
Code:
object Main extends App{
// Your code here!
// calling function
sum(20 , 50, 100)
defsum(x: Int, y : Int, z: Int){
println("This is parameter function")
var result = x + y +z
println("result obtained is :::" +result)
}
}
Output:
Example #3
In this example, we are making a mixed parameter list of a function. This takes integer and string as well.
Code:
object Main extends App{
// Your code here!
// calling function
mixedFunction(20 , 50, 100, "Ajay", "Indore")
defmixedFunction(x: Int, y : Int, z: Int, name: String, address: String){
println("This is parameter function")
var result = x + y +z
println("result obtained is :::" +result)
//now other values
println("Employee name :: " + name)
println("Employee address :: " + address)
}
}
Output:
Example #4
In this example, we are taking a user-defined object and printing its value. We can take any value as a parameter.
Code:
object Main extends App{
// Your code here!
// calling function
var emp1 = new Employee("Amita", 20, 30, "Indore")
employeeInfo(emp1)
defemployeeInfo(emp : Employee){
println("In this we are taking one user defined parameter.")
println("Passing a user value ::")
}
}
class Employee(name: String, Id:Int, Age: Int, address: String){
// your logic
}
Output:
Conclusion
Functions are used to avoid the redundant code or repetitive code. It makes our code looks simple and more understandable. Also, we can easily debug our code and identified the error, if any. These Scala functions are the same as any other programming language. Keep in mind the return type and parameter list or signature of the function while working with them.
Recommended Articles
We hope that this EDUCBA information on “Scala Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.