Scala Learning note 4--scala's function one

Source: Internet
Author: User

Default value:

def saymyname (name:string = "Jack") {println (name)}

Variable parameters:

def summoreparameters (elem:int*) = {  var sum = 0for (e <-elem) {println (e) sum + = e}  sum}

Use of underscores:

def add (a:int,b:int) = a+bdef add2  = Add (_:int,2)

Recursive functions: Need to explicitly give the return type of the function

def FAC (n:int): Int = if (n <= 0) 1 Else n*fac (n-1)

function currying:

Write this argument independently, this is currying

def mulitply (X:int) (y:int) = x*ydef m2 = mulitply (2) _;

Curry is the parameter can be separated, some of the function parameters can be underlined to replace

How anonymous functions are declared

Val t = () =>333

: followed by the data type, C for the passed in parameters

def TESTFUNC02 (c: () =>int) {      println (c ())      1000}

anonymous functions

Val D1 = (a:int) = = a+100;

The anonymous function as a parameter is actually the parameter name, followed by the argument type, then the expression

Def TESTF1 (callback: (Int,int) =>int) ={    println (Callback (123,123));}

Nested function, in fact, is a def inside a set of Def

def add3 (X:int, Y:int, z:int): int = {def add2 (X:int, y:int): int = {x + y}add2 (add2 (x, y), Z)}
def sum (f:int = int): (int, int) + int = {    def sumf (A:int, b:int): int =       if (a >b) 0 else F ( A) + SUMF (A + 1, b)     SUMF}

Scala Learning note 4--scala's function one

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.