不可或缺的函数,在Go中定义函数的方式如下: func (p myType ) funcName ( a, b int , c string ) ( r , s int ) { return } 通过函数定义,我们可以看到Go中函数和其他语言中的共性和特性 共性 关键字——func 方法名——funcName 入参——— a,b int,b string 返回值—— r,s int 函数体—— {} 特性 Go中函数的特性是非常酷的,给我们带来不一样的编程体验. 为特定类型定义函数,即为类型对象
一. 从函数返回 从函数返回就是返回语句的第一个主要用途.在程序中,有两种方法可以终止函数的执行,并返回到调用函数的位置.第一种方法是在函数体中,从第一句一直执行到最后一句,当所有语句都执行完,程序遇到结束符号”}”后返回. 例:从函数返回 #include "stdio.h" int fun(); /*声明函数*/ void main() { int a; printf("this step is before the function\n");/
定义多参数函数 - 用func声明函数 func name(parameters) -> return type { function body } func halfOpenRangeLength(start: Int, end: Int) -> Int { return end - start } let value = halfOpenRangeLength(, end: ) print(value) 定义无参数函数 func name() -> return type { fu