In this Scala Tutorial, we will discuss to define Scala Functions & Methods with the help of examples in detail.

Defining Scala Functions & Methods

 The Syntax of defining function and method are same.

Scala functions/methods start with ‘def’ keyword followed by the function name, Input parameters, colon (:) and function return type. The equal sign is a separator between function body and its signature.

Function declaration and calling are similar to any programming language which consists of function name and Input parameters.

//Function Declaration

scala> def Hello(place:String):String={“Welcome to “+place}

Hello: (place: String)String

//Function Calling

scala> Hello(“India”)

res7: String = Welcome to India

Methods are declared inside the class and accessed using the object.

scala> class Function_Demo{

 | //Method Declaration

 | def Hello(place:String):String={“Welcome to “+place} }

defined class Function_Demo

scala> val F1 = new Function_Demo

F1: Function_Demo = Function_Demo@122d6c22

//Method Calling

scala> F1.Hello(“India”)

res8: String = Welcome to India

More Discussion on Scala Functions & Methods Syntax

Please note that below discussion is valid for both functions & methods. But we will be  discussing examples of functions.

#1. Return value Using Return statement

We can return value from a function using return keyword.

scala> def example_1(a:Int,b:Int):Int={

| var k:Int=a+b;

| k=k*k+5;

| return k;

| }

exp: (a: Int, b: Int)Int

scala> example_1(2,4)

res0: Int = 41

#2. Return value without using return Statement

If we remove the return statement , the code will run without any error.

scala> def example_2(a:Int,b:Int)={

 | var k:Int=a+b;

 | k=k*k+5;

 | k;

 | }

example_2: (a: Int, b: Int)Int

scala> example_2(2,4)

res1: Int = 41

scala> def example_5(a:Int,b:Int)={

 | var k:Int=a+b;

 | “value is” +k;// It is a String

 | }

example_5: (a: Int, b: Int)String

In the above example you will find that output return type is not defined, so Scala interpreter will infer return type from the type of variable.

Please note that if we use return keyword then it is required to use the return type otherwise interpreter will give an error message.

scala> def example_3(a:Int,b:Int)={

| var k:Int=a+b;

| k=k*k+5;

| return k;

| }

<console>:13: error: method example_3 has return statement; needs result type

return k;

^

How to Install Scala on Ubuntu ? - HadoopTree
How to Install Scala on Ubuntu ? – HadoopTree

#3.  How to return Unit type?

If we don’t mention the return type and last expression of a function definition is neither a variable name or a string, then Unit type will be returned. The Unit is similar to Void in Java which means nothing will be returned.

scala> def example_4(a:Int,b:Int)={

| var k:Int=a+b;

| k=k*k+5;// Here last expression is not variable name

| }

example_4: (a: Int, b: Int)Unit

We can also add  Unit as return type.

//Here return type is mentioned as Unit

scala> def example_4(a:Int,b:Int):Unit={

| var k:Int=a+b;

| k=k*k+5;

| k;

| }

example_4: (a: Int, b: Int)Unit

#4. Ignore curly braces for single line expression

If function body contains only single line expression, then we can ignore curly braces from function definition part.

scala> def example_6(place: String): String=”Welcome to”+ place;

example_6: (place: String)String

scala> def example_7(n:Int,m:Int)=n*n+2*m*n+m*m;

example_7: (n: Int, m: Int)Int

#5. Default Input Parameters

We can also provide Default values for Input parameters. Scala function will take these values if no value is given Input parameter.

scala> def example_8(place:String=”India”):String={“Welcome to ” + place};

example_8: (place: String)String

scala> example_8()

res2: String = Welcome to India

scala> example_8(“USA”)

res3: String = Welcome to USA

#6. Variable length argument

We can also give variable length arguments as input arguments.

scala> def example_9(a:Int*)={

 | var out:Int=1;

 | for(b <- a) | out*=b; | out; } example_9: (a: Int*)Int scala> example_9(1,2,3,4,5)

res5: Int = 120

scala> example_9(1 to 5: _*)// means value from 1 to 5

res6: Int = 120

Conclusion

In this article, we started with  function/method syntax and then discussed return keyword , Unit type, default input parameters and variable length arguments.

I hope this article provide you a basic understanding of user defined Scala functions.