Cloud Computing - SCALA

Back to Course

Lesson Description

Lession - #683 SCALA FUNCTION

What is SCALA function

A function is a group of statement that play out an assignment. You can split your code into independent function. How you split your code among various function depends on you, yet consistently, the division for the most part is so that each function plays out a particular tast.

Function Declarations
A Scala Function announcement has the following structure −

def functionName ([list of parameters]>
: [return type]
Techniques are verifiably declared theoretical if you don't utilize the equivalents sign and the method body.

Function Definitions
A Scala Function definition has the following structure −


def functionName ([list of parameters]>
: [return type] = { Function body return [expr] }

Here, return type could be any valid Scala information type and list of parameters will be a list of variable isolated by comma and list of parameters and return type are optional. Basically the same as Java, a return statement can be involved alongside an statement if function returns a value. Following is the function which will add two numbers and return their total −
Language structure

object add {
   def addInt( a:Int, b:Int >
: Int = { var sum:Int = 0 aggregate = a + b bring aggregate back } }

Calling Functions
Scala gives various syntactic varieties for variation methods. following is the guideline method for calling a method −

functionName( list of parameters >
In the event that a function is being called utilizing a case of the item, we would utilize spot documentation like Java as follows −

[instance.]functionName( list of parameters >

Attempt the following model program to characterize and afterward call a similar function.


object Demo {
   def main(args: Array[String]>
{ println( "Returned Value : " + addInt(5,7>
; } def addInt( a:Int, b:Int >
: Int = { var sum:Int = 0 total = a + b bring total back } }
Save the above program in Demo.scala. The following orders are utilized to compile and execute this program.

\>scalac Demo.scala
\>scala Demo

Returned Value : 12