10.25swift函数

importFoundation

//C:封装了了⼀一段有特定功能的代码段

/*形式:

     返回值类型 函数名(参数列列表){

    代码段

    如果 返回值类型 不是void要return

}

*/

//swift函数格式:/*

func函数名(参数列列表)->返回值类型{

      功能代码段

      return...

}

*/

//调⽤用:函数名(参数列列表)

//无参无返

// (1)

func printHello()->Void{

print("hello")

}

printHello()

//(2)

func printHello1(){

print("hello--1")

}

printHello1()

//(3)

func printHello2()->(){

print("hello---2")

}


//10.25下午所学

//有参无返

/*

func函数名(参数1:数据类型,参数2:数据类型...){

代码段

}

*/

//输入月份,打印对应春(1-3),夏(4-6,秋(7-9)冬(10-12),输入月份不符合规范的则打印(找智障委员)

func month ToSeason(month:Int){

switch month{

case let temp where temp >=1&& temp <=3:

print("\n")

caselettempwheretemp >=4&& temp <=6:

print("\n")

caselettempwheretemp >=7&& temp <=9:

print("\n")

caselettempwheretemp >=10&& temp <=12:

print("\n")

default:

print("找智障委员\n")

};

}

monthToSeason(5)

//无参有返

/*

func函数名()->返回值类型{

代码段

return

}

*/

funcpeopleCount()->Int{

return19

}

//函数有返回值,所以定义一个值来接收

letcount =peopleCount()

println("count =\(count)\n")

//有参有返

/*

func函数名(参数1:数据类型1,参数2:数据类型2...)->返回值类型{

return value

}

*/

//题目1:定义一个函数,该函数输入一个月份,返回对应的季节

funcseason(month:Int)->String{

switchmonth{

caselettempwheretemp >=1&& temp <=3:

return""

caselettempwheretemp >=4&& temp <=6:

return""

caselettempwheretemp >=7&& temp <=9:

return""

caselettempwheretemp >=10&& temp <=12:

return""

default:

return"找智障委员"

};

}

vars =season(9)

print("s :\(s)\n")

//题目2:定义一个函数,该函数传入一个字符串,函数在该字符串后面拼接上一个“hello”,然后函数返回新的字符串和新字符串的长度(使用元组)

//let num:(String,Int):

funchello(str:String)->(String,Int){

letnewString = str +"hello"

letlength = newString.lengthOfBytesUsingEncoding(4)

return(newString,length)

}

lettemp =hello("lanou")

println(temp.0)

println(temp.1)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容