<span id="OSC_h2_1"></span>
1 概述
1.1 背景介绍
仓颉编程语言作为一款面向全场景应用开发的现代编程语言,通过现代语言特性的集成、全方位的编译优化和运行时实现、以及开箱即用的 IDE工具链支持,为开发者打造友好开发体验和卓越程序性能。
案例结合代码体验,让大家更直观的了解仓颉语言中的函数。
1.2 适用对象
- 个人开发者
- 高校学生
1.3 案例时间
本案例总时长预计50分钟。
1.4 案例流程
说明:
① 进入华为开发者空间,登录云主机;
② 使用CodeArts IDE for Cangjie编程和运行仓颉代码。
1.5 资源总览
资源名称 | 规格 | 单价(元) | 时长(分钟) |
---|---|---|---|
开发者空间 – 云主机 | 鲲鹏通用计算增强型 kc2 | 4vCPUs | 8G | Ubuntu | 免费 | 50 |
最新案例动态,请查阅 仓颉之函数的魔法宝典。小伙伴快来领取华为开发者空间,进入云主机桌面版实操吧!
2 运行测试环境准备
2.1 开发者空间配置
面向广大开发者群体,华为开发者空间提供一个随时访问的“开发桌面云主机”、丰富的“预配置工具集合”和灵活使用的“场景化资源池”,开发者开箱即用,快速体验华为根技术和资源。
领取云主机后可以直接进入华为开发者空间工作台界面,点击进入桌面连接云主机。没有领取在开发者空间根据指引领取配置云主机即可,云主机配置参考1.5资源总览。
2.2 创建仓颉程序
点击桌面CodeArts IDE for Cangjie,打开编辑器,点击新建工程,保持默认配置,点击创建。
产物类型说明:
- executable,可执行文件;
- static,静态库,是一组预先编译好的目标文件的集合;
- dynamic,动态库,是一种在程序运行时才被加载到内存中的库文件,多个程序共享一个动态库副本,而不是像静态库那样每个程序都包含一份完整的副本。
2.3 运行仓颉工程
创建完成后,打开src/main.cj,参考下面代码简单修改后,点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
<strong>package</strong> demo
<span style="color:#697070">// 第一个仓颉程序</span>
main(): Int64 {
<span style="color:#397300">println</span>(<span style="color:#880000">"hello world"</span>)
<span style="color:#397300">println</span>(<span style="color:#880000">"你好,仓颉!"</span>)
<strong>return</strong> <span style="color:#880000">0</span>
}
(* 注意:后续替换main.cj文件代码时,package demo保留)
(* 仓颉注释语法:// 符号之后写单行注释,也可以在一对 /* 和 */ 符号之间写多行注释)
到这里,我们第一个仓颉程序就运行成功啦!后面案例中的示例代码都可以放到main.cj文件中进行执行,接下来我们继续探索仓颉语言。
3 仓颉中的函数
3.1 定义函数
仓颉使用关键字 func 来表示函数定义的开始,func 之后依次是函数名、参数列表、可选的函数返回值类型、函数体。
函数定义举例,具体代码如下:
// 定义了一个名为add的函数
// 参数列表由两个Int64类型的参数<strong>a</strong>和<strong>b</strong>组成
// 函数返回值类型为Int64
// 函数体中将<strong>a</strong>和<strong>b</strong>相加并返回
func add(<strong>a</strong>: Int64, b: Int64): Int64 {
return <strong>a</strong> + <strong>b</strong>
}
<strong>main</strong>():Int64 {
return <span style="color:#880000">0</span>
}
参数列表:
一个函数可以有0个或者多个参数。根据函数调用时是否需要给定参数名,可以将参数列表中的参数分为两类:非命名参数和命名参数。
非命名参数的定义方式是 p: T,其中 p 表示参数名,T 表示参数 p 的类型,参数名和其类型间使用冒号连接。
命名参数的定义方式是 p!: T,与非命名参数的不同是在参数名 p 之后多了一个感叹号!。
非命名参数定义举例,具体代码如下:
<span style="color:#697070">// add函数的参数列表,定义了两个非命名参数</span>
<span style="color:#697070">// 参数名a,参数类型Int64</span>
<span style="color:#697070">// 参数名b,参数类型Int64</span>
<strong>func</strong> <strong>add</strong>(<span>a</span>: <span style="color:#880000">Int64</span>, <span>b</span>: <span style="color:#880000">Int64</span>): <span style="color:#880000">Int64</span> {
<strong>return</strong> a <span style="color:#ab5656">+</span> b
}
main():<span style="color:#880000">Int64</span> {
<strong>return</strong> <span style="color:#880000">0</span>
}
命名参数定义举例,具体代码如下:
// add函数的参数列表,定义了两个命名参数
// 命名参数<strong>a</strong>,参数类型Int64,并设置默认值
// 命名参数<strong>b</strong>,参数类型Int64,并设置默认值
func add(<strong>a</strong>!: Int64 = <span style="color:#880000">1</span>, b!: Int64 = <span style="color:#880000">1</span>): Int64 {
return <strong>a</strong> + <strong>b</strong>
}
<strong>main</strong>():Int64 {
return <span style="color:#880000">0</span>
}
非命名参数和命名参数使用注意事项:
- 只能为命名参数设置默认值,不能为非命名参数设置默认值;
- 参数列表中可以同时定义非命名参数和命名参数,非命名参数只能定义在命名参数之前;
- 函数参数均为不可变变量,在函数定义内不能对其赋值。
函数返回值类型:
函数返回值类型是函数被调用后得到的值的类型。函数定义时,返回值类型是可选的:可以显式地定义返回值类型,也可以不定义返回值类型,交由编译器推导确定。
函数返回值类型举例,具体代码如下:
<span style="color:#697070">// 定义了一个名为add的函数</span>
<span style="color:#697070">// 函数返回值类型为Int64</span>
<strong>func</strong> <strong>add</strong>(<span>a</span>: <span style="color:#880000">Int64</span>, <span>b</span>: <span style="color:#880000">Int64</span>): <span style="color:#880000">Int64</span> {
<strong>return</strong> a <span style="color:#ab5656">+</span> b
}
main():<span style="color:#880000">Int64</span> {
<strong>return</strong> <span style="color:#880000">0</span>
}
函数体:
函数体中定义了函数被调用时执行的操作,通常包含一系列的变量定义和表达式,也可以包含新的函数定义(即嵌套函数)。如下 add 函数的函数体中首先定义了 Int64 类型的变量 r(初始值为 0),接着将 a + b 的值赋值给 r,最后将 r 的值返回。
有关函数体的举例,具体代码如下:
<strong>func</strong> <strong>add</strong>(<span>a</span>: <span style="color:#880000">Int64</span>, <span>b</span>: <span style="color:#880000">Int64</span>) {
<strong>var</strong> r <span style="color:#ab5656">=</span> <span style="color:#880000">0</span>
r <span style="color:#ab5656">=</span> a <span style="color:#ab5656">+</span> b
<strong>return</strong> r
}
main():<span style="color:#880000">Int64</span> {
<strong>return</strong> <span style="color:#880000">0</span>
}
3.2 调用函数
非命名参数调用举例,具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<span style="color:#697070">// add函数,参数为非命名参数</span>
func <strong>add</strong>(<span>a: Int64, b: Int64</span>) {
<strong>return</strong> a + b
}
<strong>main</strong>() {
<strong>let</strong> x = <span style="color:#880000">1</span>
<strong>let</strong> y = <span style="color:#880000">2</span>
<span style="color:#697070">// 调用add函数</span>
<strong>let</strong> r = <strong>add</strong>(x, y)
<strong>println</strong>(<span style="color:#880000">"The sum of x and y is ${r}"</span>)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
命名参数调用举例,具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<span style="color:#697070">// add函数,非命名参数a,命名参数b</span>
func <strong>add</strong>(<span>a: Int64, b!: Int64</span>) {
<strong>return</strong> a + b
}
<strong>main</strong>() {
<strong>let</strong> x = <span style="color:#880000">1</span>
<strong>let</strong> y = <span style="color:#880000">2</span>
<span style="color:#697070">// 调用函数</span>
<strong>let</strong> r = <strong>add</strong>(x, <span>b</span>: y)
<strong>println</strong>(<span style="color:#880000">"The sum of x and y is ${r}"</span>)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
对于多个命名参数,调用时的传参顺序可以和定义时的参数顺序不同。
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<span style="color:#697070">// 两个参数都是命名参数</span>
func <strong>add</strong>(<span>a!: Int64, b!: Int64</span>) {
<strong>return</strong> a + b
}
<strong>main</strong>() {
<strong>let</strong> x = <span style="color:#880000">1</span>
<strong>let</strong> y = <span style="color:#880000">2</span>
<span style="color:#697070">// 调用函数,传参顺序可以不同</span>
<strong>let</strong> r = <strong>add</strong>(<span>b</span>: y, <span>a</span>: x)
<strong>println</strong>(<span style="color:#880000">"The sum of x and y is ${r}"</span>)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
对于拥有默认值的命名参数,调用时如果没有传实参,那么此参数将使用默认值作为实参的值,具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<span style="color:#697070">// 函数add,非命名参数a,命名参数b,b有默认值</span>
func <strong>add</strong>(<span>a: Int64, b!: Int64 = <span style="color:#880000">9</span></span>) {
<strong>return</strong> a + b
}
<strong>main</strong>() {
<strong>let</strong> x = <span style="color:#880000">1</span>
<span style="color:#697070">// 调用时没有传实参,以默认值作为实参的值</span>
<strong>let</strong> r = <strong>add</strong>(x)
<strong>println</strong>(<span style="color:#880000">"The sum of x and y is ${r}"</span>)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
对于拥有默认值的命名参数,调用时也可以为其传递新的实参,此时命名参数的值等于新的实参的值,即定义时的默认值将失效。
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<span style="color:#697070">// 函数add,非命名参数a,命名参数b,b有默认值</span>
func <strong>add</strong>(<span>a: Int64, b!: Int64 = <span style="color:#880000">9</span></span>) {
<strong>return</strong> a + b
}
<strong>main</strong>() {
<strong>let</strong> x = <span style="color:#880000">5</span>
<span style="color:#697070">// 调用时,传递新的实参,默认值将失效</span>
<strong>let</strong> r = <strong>add</strong>(x, <span>b</span>: <span style="color:#880000">20</span>)
<strong>println</strong>(<span style="color:#880000">"The sum of x and y is ${r}"</span>)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
3.3 函数类型
仓颉编程语言中,函数是一等公民,可以作为函数的参数或返回值,也可以赋值给变量。因此函数本身也有类型,称之为函数类型。
函数类型由函数的参数类型和返回类型组成,参数类型和返回类型之间使用 -> 连接。
函数类型举例,具体代码如下:
<span><strong>func</strong> <strong>hello</strong>
<span>()</span></span>: Unit {
<span style="color:#397300">println</span>(<span style="color:#880000">"Hello!"</span>)
}
上述示例定义了一个函数,函数名为 hello,其类型是 () -> Unit,表示该函数没有参数,返回类型为 Unit。
函数类型另外示例,具体代码如下:
func add(<strong>a</strong>: Int64, b: Int64): Int64 {
<strong>a</strong> + <strong>b</strong>
}
函数名为 add,其类型是 (Int64, Int64) -> Int64,表示该函数有两个参数,两个参数类型均为 Int64,返回类型为 Int64。
函数类型的类型参数:
可以为函数类型标记显式的类型参数名,下面例子中的 name 和 price 就是类型参数名。
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<span style="color:#697070">/*
函数名:showFruitPrice
参数:name 和 price
函数返回值类型是:Unit
*/</span>
func <strong>showFruitPrice</strong>(<span>name: <span style="color:#397300">String</span>, price: Int64</span>) {
<strong>println</strong>(<span style="color:#880000">"fruit: ${name} price: ${price} yuan"</span>)
}
<strong>main</strong>() {
<span style="color:#697070">// 定义变量 fruitPriceHandler</span>
<span style="color:#697070">// 变量类型为函数类型:(name: String, price: Int64) -> Unit</span>
<strong>let</strong> <span>fruitPriceHandler</span>: (<span>name</span>: <strong>String</strong>, <span>price</span>: <strong>Int64</strong>) -> <strong>Unit</strong>
<span style="color:#697070">// 赋值</span>
fruitPriceHandler = showFruitPrice
<span style="color:#697070">// 调用函数</span>
<strong>fruitPriceHandler</strong>(<span style="color:#880000">"banana"</span>, <span style="color:#880000">10</span>)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
注意:对于一个函数类型,只允许统一写类型参数名,或者统一不写类型参数名,不能交替存在。
函数类型作为参数类型:
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<span style="color:#697070">// 表示该函数有两个参数,两个参数类型均为Int64,返回类型为Int64</span>
<strong>func</strong> <strong>add</strong>(<strong>a</strong>: Int64, <strong>b</strong>: Int64): <strong>Int64</strong> {
<strong>a</strong> + <strong>b</strong>
}
<span style="color:#697070">/*
函数名为:printAdd
其类型是:((Int64, Int64) -> Int64, Int64, Int64) -> Unit
表示该函数有三个参数,参数类型分别为函数类型 (Int64, Int64) -> Int64和两个Int64,返回类型为Unit
*/</span>
<strong>func</strong> <strong>printAdd</strong>(<strong>add</strong>: (Int64, Int64) -> Int64, <strong>a</strong>: Int64, <strong>b</strong>: Int64): <strong>Unit</strong> {
<strong>println</strong>(<span style="color:#397300">add</span>(a, b))
}
<strong>main</strong>() {
<span style="color:#697070">// 函数调用</span>
<strong>printAdd</strong>(add,<span style="color:#880000">5</span>,<span style="color:#880000">5</span>)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
函数类型作为返回值类型:
函数名为 returnAdd,其类型是 () -> (Int64, Int64) -> Int64,表示该函数无参数,返回类型为函数类型 (Int64, Int64) -> Int64。
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<strong>func</strong> <strong>add</strong>(<span>a</span>: <span style="color:#880000">Int64</span>, <span>b</span>: <span style="color:#880000">Int64</span>): <span style="color:#880000">Int64</span> {
a <span style="color:#ab5656">+</span> b
}
<span style="color:#697070">/*
函数名:returnAdd
无参数
返回类型为函数类型:(Int64, Int64) -> Int64
*/</span>
<strong>func</strong> <strong>returnAdd</strong>(): (<span style="color:#880000">Int64</span>, <span style="color:#880000">Int64</span>) -> <span style="color:#880000">Int64</span> {
add
}
main() {
<strong>var</strong> x <span style="color:#ab5656">=</span> returnAdd()
println(x(<span style="color:#880000">1</span>,<span style="color:#880000">2</span>))
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
3.4 嵌套函数
定义在源文件顶层的函数被称为全局函数。定义在函数体内的函数被称为嵌套函数。
函数 foo 内定义了一个嵌套函数 nestAdd,可以在 foo 内调用该嵌套函数 nestAdd,也可以将嵌套函数 nestAdd 作为返回值返回,在 foo 外对其进行调用。
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
func <strong>foo</strong>() {
func <strong>nestAdd</strong>(<span>a: Int64, b: Int64</span>) {
a + b + <span style="color:#880000">3</span>
}
<strong>println</strong>(<strong>nestAdd</strong>(<span style="color:#880000">1</span>, <span style="color:#880000">2</span>))
<strong>return</strong> nestAdd
}
<strong>main</strong>() {
<strong>let</strong> f = <strong>foo</strong>()
<strong>let</strong> x = <strong>f</strong>(<span style="color:#880000">1</span>, <span style="color:#880000">2</span>)
<strong>println</strong>(<span style="color:#880000">"result: ${x}"</span>)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
3.5 Lambda表达式
Lambda 表达式定义:
Lambda 表达式的语法为如下形式: { p1: T1, …, pn: Tn => expressions | declarations }。
其中,=> 之前为参数列表,多个参数之间使用 , 分隔,每个参数名和参数类型之间使用 : 分隔。=> 之前也可以没有参数。=> 之后为 lambda 表达式体,是一组表达式或声明序列。
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<strong>main</strong>() {
<span style="color:#697070">// 定义Lambda表达式,直接调用</span>
<strong>let</strong> r1 = { <span>a</span>: <strong>Int64</strong>, <span>b</span>: <span><span>Int64</span> =></span> a * b }(<span style="color:#880000">1</span>, <span style="color:#880000">2</span>)
<strong>println</strong>(<span style="color:#880000">"result: ${r1}"</span>)
<span style="color:#697070">// 赋值给变量,通过变量名进行调用</span>
<strong>let</strong> r2 = { <span>a</span>: <strong>Int64</strong>, <span>b</span>: <span><span>Int64</span> =></span> a + b }
<strong>let</strong> r3 = <strong>r2</strong>(<span style="color:#880000">1</span>,<span style="color:#880000">2</span>)
<strong>println</strong>(<span style="color:#880000">"result: ${r3}"</span>)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
3.6 闭包
一个函数或 lambda 从定义它的静态作用域中捕获了变量,函数或 lambda 和捕获的变量一起被称为一个闭包,这样即使脱离了闭包定义所在的作用域,闭包也能正常运行。
闭包 add,捕获了 let 声明的局部变量 num,之后通过返回值返回到 num 定义的作用域之外,调用 add 时仍可正常访问 num。
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<span style="color:#697070">// 定义函数 returnAddNum,返回值类型是一个函数</span>
<strong>func</strong> <strong>returnAddNum</strong>(): (<span style="color:#880000">Int64</span>) -> <span style="color:#880000">Int64</span> {
<strong>let</strong> num: <span style="color:#880000">Int64</span> <span style="color:#ab5656">=</span> <span style="color:#880000">10</span>
<strong>func</strong> <strong>add</strong>(<span>a</span>: <span style="color:#880000">Int64</span>) {
<strong>return</strong> a <span style="color:#ab5656">+</span> num
}
add
}
main() {
<strong>let</strong> f <span style="color:#ab5656">=</span> returnAddNum()
println(f(<span style="color:#880000">10</span>))
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
3.7 函数调用语法糖
流操作符包括两种:表示数据流向的中缀操作符 |> (称为 pipeline)和表示函数组合的中缀操作符 ~> (称为 composition)。
尾随 lambda:
尾随 lambda 可以使函数的调用看起来像是语言内置的语法一样,增加语言的可扩展性。
当函数最后一个形参是函数类型,并且函数调用对应的实参是 lambda 时,可以使用尾随 lambda 语法,将 lambda 放在函数调用的尾部,圆括号外面。
下例中定义了一个 myIf 函数,它的第一个参数是 Bool 类型,第二个参数是函数类型。当第一个参数的值为 true 时,返回第二个参数调用后的值,否则返回 0。调用 myIf 时可以像普通函数一样调用,也可以使用尾随 lambda 的方式调用。
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<span style="color:#697070">// 定义函数myIf,第一个参数是Bool类型,第二个参数是函数类型</span>
func <strong>myIf</strong>(<span>a: Bool, fn: () -> Int64</span>) {
<strong>if</strong>(a) {
<strong>fn</strong>()
} <strong>else</strong> {
<span style="color:#880000">0</span>
}
}
func <strong>test</strong>() {
<span style="color:#697070">// 普通函数调用</span>
<strong>let</strong> a = <strong>myIf</strong>(<span style="color:#669955">true</span>, { => <span style="color:#880000">100</span> })
<strong>println</strong>(<span style="color:#880000">"result: ${a}"</span>)
<span style="color:#697070">// 尾随 lambda 的方式调用</span>
<strong>let</strong> b = <strong>myIf</strong>(<span><span style="color:#669955">true</span></span>) {<span style="color:#880000">10</span>}
<strong>println</strong>(<span style="color:#880000">"result: ${b}"</span>)
}
<strong>main</strong>() {
<strong>test</strong>()
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
Pipeline表达式:
当需要对输入数据做一系列的处理时,可以使用 pipeline 表达式来简化描述。pipeline 表达式的语法形式如下:e1 |> e2。等价于如下形式的语法糖:let v = e1; e2(v) 。
其中 e2 是函数类型的表达式,e1 的类型是 e2 的参数类型的子类型。
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
<span style="color:#697070">// 数组中的每个元素都加1</span>
<strong>func</strong> <strong>inc</strong>(<span>x</span>: <span style="color:#880000">Array</span><<span style="color:#880000">Int64</span>>): <span style="color:#880000">Array</span><<span style="color:#880000">Int64</span>> {
<strong>let</strong> s <span style="color:#ab5656">=</span> x.size
<strong>var</strong> i <span style="color:#ab5656">=</span> <span style="color:#880000">0</span>
<strong>for</strong> (e <strong>in</strong> x <strong>where</strong> i <span style="color:#ab5656"><</span> s) {
x[i] <span style="color:#ab5656">=</span> e <span style="color:#ab5656">+</span> <span style="color:#880000">1</span>
i<span style="color:#ab5656">++</span>
}
x
}
<span style="color:#697070">// 数组元素求和</span>
<strong>func</strong> <strong>sum</strong>(<span>y</span>: <span style="color:#880000">Array</span><<span style="color:#880000">Int64</span>>): <span style="color:#880000">Int64</span> {
<strong>var</strong> s <span style="color:#ab5656">=</span> <span style="color:#880000">0</span>
<strong>for</strong> (j <strong>in</strong> y) {
s <span style="color:#ab5656">+=</span> j
}
s
}
main() {
<strong>let</strong> arr: <span style="color:#880000">Array</span><<span style="color:#880000">Int64</span>> <span style="color:#ab5656">=</span> <span style="color:#880000">Array</span><<span style="color:#880000">Int64</span>>([<span style="color:#880000">1</span>, <span style="color:#880000">3</span>, <span style="color:#880000">5</span>])
<span style="color:#697070">// pipeline 表达式</span>
<span style="color:#697070">// 等价于,先把arr传入inc函数,再把inc函数的返回值传入sum函数</span>
<strong>let</strong> res <span style="color:#ab5656">=</span> arr <span style="color:#ab5656">|></span> inc <span style="color:#ab5656">|></span> sum
println(<span style="color:#880000">"result: ${res}"</span>)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
composition表达式:
composition 表达式表示两个单参函数的组合。composition 表达式语法如下: f ~> g。等价于如下形式: { x => g(f(x)) }。
其中 f,g 均为只有一个参数的函数类型的表达式。
f 和 g 组合,则要求 f(x) 的返回类型是 g(…) 的参数类型的子类型。
具体代码如下:
<strong>func</strong> <strong>f</strong>(<span>x</span>: <span style="color:#880000">Int64</span>): <span style="color:#880000">Float64</span> {
<span style="color:#880000">Float64</span>(x)
}
<strong>func</strong> <strong>g</strong>(<span>x</span>: <span style="color:#880000">Float64</span>): <span style="color:#880000">Float64</span> {
x
}
main() {
<span style="color:#697070">// The same as { x: Int64 => g(f(x)) }</span>
<strong>var</strong> fg <span style="color:#ab5656">=</span> f <span style="color:#ab5656">~></span> g
}
3.8 函数重载
函数重载定义?
在仓颉编程语言中,如果一个作用域中,一个函数名对应多个函数定义,这种现象称为函数重载。
- 函数名相同,函数参数不同的两个函数构成重载。
具体代码如下:
<span style="color:#697070">// 函数名相同,函数参数不同,构成重载</span>
<strong>func</strong> <strong>f</strong>(<span>a</span>: <span style="color:#880000">Int64</span>): <span style="color:#880000">Unit</span> {
}
<strong>func</strong> <strong>f</strong>(<span>a</span>: <span style="color:#880000">Float64</span>): <span style="color:#880000">Unit</span> {
}
<strong>func</strong> <strong>f</strong>(<span>a</span>: <span style="color:#880000">Int64</span>, <span>b</span>: <span style="color:#880000">Float64</span>): <span style="color:#880000">Unit</span> {
}
- 同一个类内的两个构造函数参数不同,构成重载。
具体代码如下:
<span style="color:#697070">// 两个构造函数参数不同,构成重载</span>
<strong>class</strong> <strong>C</strong> {
<strong>var</strong> a: Int64
<strong>var</strong> b: Float64
<strong>public</strong> <strong>init</strong>(a: Int64, b: Float64) {
<strong>this</strong>.a = a
<strong>this</strong>.b = b
}
<strong>public</strong> <strong>init</strong>(a: Int64) {
b = <span style="color:#880000">0.0</span>
<strong>this</strong>.a = a
}
}
- 同一个类内的主构造函数和 init 构造函数参数不同,构成重载。
具体代码如下:
<span style="color:#697070">// 同一个类内的主构造函数和 init 构造函数参数不同,构成重载</span>
<strong>class</strong> <strong>C</strong> {
C(<strong>var</strong> a!: Int64, <strong>var</strong> b!: Float64) {
<strong>this</strong>.a = a
<strong>this</strong>.b = b
}
<strong>public</strong> <strong>init</strong>(a: Int64) {
b = <span style="color:#880000">0.0</span>
<strong>this</strong>.a = a
}
}
- 两个函数名相同,参数不同的函数定义在不同的作用域,在两个函数都可见的作用域中构成重载。
具体代码如下:
<span style="color:#697070">// 两个函数名相同,参数不同的函数定义在不同的作用域,在两个函数都可见的作用域中构成重载</span>
<strong>func</strong> <strong>f</strong>(<span>a</span>: <span style="color:#880000">Int64</span>): <span style="color:#880000">Unit</span> {
}
<strong>func</strong> <strong>g</strong>() {
<strong>func</strong> <strong>f</strong>(<span>a</span>: <span style="color:#880000">Float64</span>): <span style="color:#880000">Unit</span> {
}
}
- 如果子类中存在与父类同名的函数,并且函数的参数类型不同,则构成函数重载。
具体代码如下:
<span style="color:#697070">// 子类中存在与父类同名的函数,并且函数的参数类型不同,则构成函数重载</span>
<strong>open</strong> <strong>class</strong> <strong>Base</strong> {
<strong>public</strong> <strong>func</strong> <strong>f</strong>(<span>a</span>: <span style="color:#880000">Int64</span>): <span style="color:#880000">Unit</span> {
}
}
<strong>class</strong> <strong>Sub</strong> <: <strong>Base</strong> {
<strong>public</strong> <strong>func</strong> <strong>f</strong>(<span>a</span>: <span style="color:#880000">Float64</span>): <span style="color:#880000">Unit</span> {
}
}
函数重载决议
函数调用时,所有可被调用的函数构成候选集,候选集中有多个函数,究竟选择候选集中哪个函数,需要进行函数重载决议,有如下规则:
- 优先选择作用域级别高的作用域内的函数;
- 如果作用域级别相对最高的仍有多个函数,则需要选择最匹配的函数;
- 子类和父类认为是同一作用域,选择更匹配的父类中定义的函数
3.9 const函数
const 函数是一类特殊的函数,这些函数具备了可以在编译时求值的能力。在 const 上下文中调用这种函数时,这些函数会在编译时执行计算。而在其它非 const 上下文,const 函数会和普通函数一样在运行时执行。
下例是一个计算平面上两点距离的 const 函数,distance 中使用 let 定义了两个局部变量 dx 和 dy。
具体代码操作如下:
Step1:复制以下代码,替换main.cj文件中的代码。(保留package)
结构类型struct的定义以关键字struct开头,后跟struct的名字,接着是定义在一对花括号中的 struct 定义体。struct 定义体中可以定义一系列的成员变量、成员属性、静态初始化器、构造函数和成员函数。
struct <strong>Point</strong> {
<strong>const</strong> <strong>Point</strong>(<strong>let</strong> <span>x</span>: <strong>Float64</strong>, <strong>let</strong> <span>y</span>: <strong>Float64</strong>) {}
}
<strong>const</strong> func <strong>distance</strong>(<span>a: Point, b: Point</span>) {
<strong>let</strong> dx = a.<span>x</span> - b.<span>x</span>
<strong>let</strong> dy = a.<span>y</span> - b.<span>y</span>
(dx**<span style="color:#880000">2</span> + dy**<span style="color:#880000">2</span>)**<span style="color:#880000">0.5</span>
}
<strong>main</strong>() {
<strong>const</strong> a = <strong>Point</strong>(<span style="color:#880000">3.0</span>, <span style="color:#880000">0.0</span>)
<strong>const</strong> b = <strong>Point</strong>(<span style="color:#880000">0.0</span>, <span style="color:#880000">4.0</span>)
<strong>const</strong> d = <strong>distance</strong>(a, b)
<strong>println</strong>(d)
}
Step2:点击编辑器右上角运行按钮直接运行,终端窗口可以看到打印内容。
至此,仓颉之函数的魔法宝典案例内容已全部完成。
如果想了解更多仓颉编程语言知识可以访问:https://cangjie-lang.cn/
</div>