2, entry function

First, the entry function

1. Defined Functions

When Scala defined function, you need to define the function name, parameters, functions Functions. 

Our first function is as follows: 
  DEF the sayHello (name: String, Age: Int) = { 
    IF (Age> 18 is) {the printf ( "% A Hi Big Boy \ n-S, you are", name); Age } 
   the else {the printf ( "% S Hi, you are a Little Boy \ n-", name);} Age 

Scala requirements must be given to all type parameters, but do not necessarily give the type of the function return value, as long as the function of the right body statement does not contain recursion, Scala can infer the return type according to their expression on the right. 



######### 
Scala>: Paste 
// MODE Entering Paste (Ctrl-D to Finish) 

DEF the sayHello (name: String, Age: Int) = { 
 IF (Age> = 18 is) { 
  the printf ( "the Hi ,% \ n-S, A Big Boy are you! ", name) 
  Age 
 } {the else 
  the printf (" the Hi,% \ n-"S, A Little Boy you are!, name) 
  Age 
 } 
} 

// the Exiting Paste MODE, now interpreting.

sayHello: (name: String, age: Int)Int


scala> sayHello("leo", 30)
Hi, leo, you are a big boy!
res40: Int = 30


2, the function is defined in the code block

One-way function: def sayHello (name: String) = print ( "Hello," + name) 

If multiple lines of code in the body of a function, you can use the code block manner parcel lines of code, code block the return value of the last line the return value is the entire function. 
Unlike Java, instead of using the return value return. 

Functions such as, for accumulator function: 
DEF SUM (n-: Int) = { 
  var SUM = 0; 
  for (I < - . 1 to n-) + SUM = I 
   SUM 
} 


###### 
Scala > : Paste 
// Paste MODE Entering (Ctrl-D to Finish) 

DEF SUM (n-: Int) = = {# when written, the return value of the function have 
 var Result = 0 
 for (I < - . 1 to n-) { 
  Result + = I
  } 
 Result 
} 

Exiting Paste the MODE //, now Interpreting. 

SUM: (the n-: Int) Int 


Scala> sum(10)
res41: Int = 55


3, recursive functions

If the recursive function is a function of the body itself, you must manually return type of the function is given. 

For example, to achieve the classic Fibonacci number: 
DEF Fab (n-: Int): Int = {# Int second return type 
IF (n- < =. 1 ). 1 
the else Fab (. 1-n-) + Fab (N- 2) 
}


Second, the default parameters and parameters with a name

1, default parameters

In Scala, sometimes we call certain functions, we do not want to give specific values of the parameters, and want to use their own parameter default values, then you use the definition of default parameters when defining the function. 

the sayHello DEF (firstName: String, middleName: String = "William", 
lastName: String = "Croft") = firstName + "" + middleName + "" + 
lastName 

parameter if not given, it will be sequentially applied from the right parameter. 




#### 
Scala>: Paste 
// MODE Entering Paste (Ctrl-D to Finish) 

DEF the sayHello (name: String, Age: Int = 20 is) { 
 Print ( "the Hello," + name + ", you IS Age" + Age) 
} 

// Exiting the MODE Paste, now Interpreting. 

sayHello: (name: String, Age: Int) Unit 

Scala> sayHello ( "LEO") 
the Hello, LEO, you Age 20 IS 
Scala> sayHello ( "LEO", 30) 
Hello, leo, you age is 30


2, with the name of the parameter

When calling the function, or may not function in accordance with the order defined by the parameters to pass parameters to pass but with use of the parameter name. 

sayHello (firstName = "Mick", lastName = "Nina", middleName = "Jack") 

may also be used in combination with not named and the name of the parameter, the parameter to be named but not in the front row with parameter name. 

the sayHello ( "Mick", lastName = "Nina", middleName = "Jack") 



#####


Third, vararg

1, vararg

In Scala, we may need to function as a variable number of parameters defined form, at this time can use variable length parameter defined function. 
SUM DEF (the nums: Int *) = { 
  var RES = 0 
  for (NUM < - the nums) + RES = NUM
   RES 
} 



####### 
Scala > : Paste 
// MODE Entering Paste (Ctrl-D to Finish) 

SUM DEF (the nums: Int *) = { 
 var Result = 0 
 for (NUM < - the nums) { 
  Result + = NUM
  } 
 Result 
} 

// the Exiting Paste MODE, now Interpreting. 

SUM: (the nums: Int *) Int 

Scala > SUM (1,2,3,4,5) 
res44: Int = 15


2, the use of variable length parameter sequence calls

If you want to have in a sequence of variable-length argument function called directly, it is wrong. For example val s = sum (1 to 5 ). 
Scala case requires special syntax parameters are defined as a sequence, so that the interpreter can identify Scala. This syntax is very useful! You must be a good idea to use a lot of spark to the source code. 

val s = sum (1 to 5 : _ *) 

Case: recursive function is used for an accumulation 

DEF SUM2 (the nums: Int *): Int = { 
  IF (nums.length == 0) 0 
  the else nums.head + SUM2 (the nums. tail: _ *) 
} 



######## 
Scala>: Paste 
// MODE Entering Paste (Ctrl-D to Finish) 

DEF SUM (the nums: Int *) = { 
 var Result = 0 
 for (NUM < - the nums ) { 
  Result + = NUM
  } 
 Result 
} 

// the Exiting Paste MODE, now Interpreting. 

SUM: (the nums: Int *) Int 


Scala >  SUM (. 1. 5 to: _ *)
res45: Int = 15 


##
scala> :paste
// Entering paste mode (ctrl-D to finish)

def sum2(nums: Int*): Int = {
 if(nums.length == 0) 0
 else nums.head + sum2(nums.tail: _*)
}

// Exiting paste mode, now interpreting.

sum2: (nums: Int*)Int

scala> sum2(1,2,3,4,5)
res46: Int = 15


Fourth, the process, lazy values ​​and exceptions

1, process

In Scala, defining a function, if the function body directly wrapped in curly braces, without the use = connection type of the function return value is the Unit. Such a function is called to process. 
Function does not require the process commonly used for the return value. 

There is also a process of writing, is the function's return value type is defined as Unit. 

DEF the sayHello (name: String) = "the Hello," + name 
DEF the sayHello (name: String) {Print ( "the Hello," + name); "the Hello," + name} 
DEF the sayHello (name: String): Unit = " hello, "name + 




###### 
Scala> DEF the sayHello (name: String) {Print (" Hello, "+ name);" hello, "name} + 
the sayHello: (name: String) Unit 

Scala> the sayHello ( "LEO") 
Hello, LEO 


Scala> DEF the sayHello (name: String) = {Print ( "Hello," + name); "the Hello," + name} 
the sayHello: (name: String) String 

Scala> the sayHello ( "LEO" ) 
the Hello, leores48: String = the Hello,




scale>


2, lazy value

In Scala, provides features lazy value, that is, if a variable is declared as a lazy, only the first use of the variable, the variable corresponding to the expression evaluation will occur. 
This particular feature is particularly useful for the calculation of time-consuming operations, such as opening files lO, lO network like. 

scala.io.Source._ Import 
lazy Val Lines = fromFile ( "C: /Users/Administrator/Desktop/test.txt") mkString. 

Even if the file does not exist, no error will, will complain when only the first using a variable proved lazy characteristic expression calculation. 

Lines of fromFile = Val ( "C: //Users//Administrator/Desktop/test.txt") .mkString 
the lazy Val Lines of fromFile = ( "C: //Users/Administrator//Desktop/test.txt") .mkString 
DEF of fromFile = Lines. ( "C:. // the Users / Administrator / Desktop / TXT Test") mkString 




####### 
Scala> = the lazy Val Lines of fromFile ( "Home // // test.txt") mkString. 
Lines: String = < the lazy > 

Scala> Print (Lines) 
the Hello Word


3, abnormal

In Scala, exception handling and capture mechanism is very similar to Java. 

{the try 
  the throw new new lllegalArgumentException ( "X Should Not BE negative") 
} {the catch 
  Case _: = lllegalArgumentException> Printin ( "lllegal the Argument!") 
} the finally { 
  Print ( "Release Resources!") 



######## #####

Guess you like

Origin www.cnblogs.com/weiyiming007/p/10954018.html