---
title: "Scala函数和代码块"
author: "cakin24"
date: "2017-08-20T08:01:18Z"
tags: ["代码块"]
categories: ["编程语言"]
slug: "scala"
draft: false
lastmod: "2017-08-20T08:10:51Z"
id: 2390434
url: "https://cakin24.iteye.com/blog/2390434"
---

**一 代码块**

{exp1;exp2}

{

exp1

exp2

}

代码块也是一个表达式，其最终的求值是最后一个表达式的值。

 

**二 函数**

def functionName(param:ParamType):ReturnType={

//function body:expressions

}

 

**三 实例**

01. `object func_examples {`
02. `def hello(name:String):String={`
03. `s"Hello,${name}"`
04. `}//> hello: (name: String)String`
05. `hello("cakin24")//> res0: String = Hello,cakin24`
06. `def hello2(name:String)={`
07. `s"Hello,${name}"`
08. `}//> hello2: (name: String)String`
09. `hello("cakin24")//> res1: String = Hello,cakin24`
10. `def Add(x:Int,y:Int)=x+y //> Add: (x: Int, y: Int)Int`
11. `Add(1,2)//> res2: Int = 3`
12. `}`