10.25swift函數(shù)

importFoundation

//C:封裝了了?一段有特定功能的代碼段

/*形式:

? ? ?返回值類型 函數(shù)名(參數(shù)列列表){

? ? 代碼段

? ? 如果 返回值類型 不是void要return

}

*/

//swift函數(shù)格式:/*

func函數(shù)名(參數(shù)列列表)->返回值類型{

? ? ? 功能代碼段

? ? ? return...

}

*/

//調(diào)?用:函數(shù)名(參數(shù)列列表)

//無參無返

// (1)

func printHello()->Void{

print("hello")

}

printHello()

//(2)

func printHello1(){

print("hello--1")

}

printHello1()

//(3)

func printHello2()->(){

print("hello---2")

}


//10.25下午所學

//有參無返

/*

func函數(shù)名(參數(shù)1:數(shù)據(jù)類型,參數(shù)2:數(shù)據(jù)類型...){

代碼段

}

*/

//輸入月份,打印對應春(1-3),夏(4-6,秋(7-9)冬(10-12),輸入月份不符合規(guī)范的則打印(找智障委員)

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函數(shù)名()->返回值類型{

代碼段

return

}

*/

funcpeopleCount()->Int{

return19

}

//函數(shù)有返回值,所以定義一個值來接收

letcount =peopleCount()

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

//有參有返

/*

func函數(shù)名(參數(shù)1:數(shù)據(jù)類型1,參數(shù)2:數(shù)據(jù)類型2...)->返回值類型{

return value

}

*/

//題目1:定義一個函數(shù),該函數(shù)輸入一個月份,返回對應的季節(jié)

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:定義一個函數(shù),該函數(shù)傳入一個字符串,函數(shù)在該字符串后面拼接上一個“hello”,然后函數(shù)返回新的字符串和新字符串的長度(使用元組)

//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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容