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)