Last active
June 17, 2016 12:48
-
-
Save gitbricho/e52c2646f728ee0598b6 to your computer and use it in GitHub Desktop.
swift:func
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
//## シンタックス: (パラメータリスト) -> (戻り値) | |
//## 定義と呼び出し | |
// 1. (String) -> (String) | |
func f挨拶(名前: String) -> String { | |
let l挨拶 = 名前 + "さん、今日は" | |
return l挨拶 | |
} | |
// 呼び出し | |
print(f挨拶(名前: "優子")) //結果:優子さん、今日は | |
// 2.引数無し: () -> (String) | |
func f引数なしの挨拶() -> String { | |
return "皆さん、今日は" | |
} | |
print(f引数なしの挨拶()) //結果:皆さん、今日は | |
// 3.戻り値なし: (String) -> () | |
func f挨拶を表示(名前: String) { | |
print("\(名前)さん、今日は") | |
} | |
f挨拶を表示(名前: "健二") //健二さん、今日は | |
// 4. (Int, Int) -> (Int) | |
func f合計(x: Int, y: Int) -> (Int) { | |
return x + y | |
} | |
f合計(x:5, y:10) //15 | |
// 5. (Double) -> (Double) | |
func f華氏摂氏変換(華氏:Double) -> Double { | |
return (((華氏 - 32) * 5) / 9) | |
} | |
print("華氏720度は摂氏\(f華氏摂氏変換(華氏:720))") | |
// 結果: 華氏720度は摂氏382.222222222222 | |
// 6. (String, Double) -> (Double) | |
func f温度変換(変換タイプ:String, 温度:Double) -> Double? { | |
switch 変換タイプ { | |
case "華氏": | |
return (((温度 * 9) / 5) + 32) | |
case "摂氏": | |
return (((温度 - 32) * 5) / 9) | |
default: | |
return nil | |
} | |
} | |
print("華氏720度は摂氏\(f温度変換(変換タイプ:"摂氏", 温度: 720)!)") | |
// 結果: 華氏720度は摂氏382.222222222222 | |
// 7. 複数パラメータ | |
func add(_ a:Int, b:Int) -> Int { | |
return a + b | |
} | |
add(6, b: 7) //13 | |
func add(_ a:Int, _ b:Int) -> Int { | |
return a + b | |
} | |
add(6, 7) //13 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
//## 関数を戻り値として返す: | |
// () -> ((String, Int) -> String) | |
func f関数を返す関数() -> ((String, Int)->String) { | |
//入れ子の関数。 | |
//update: 16/06/15 | |
func f指定位置を大文字にする(_ text:String, _ pos: Int) -> (String) { | |
//操作する文字列の範囲 | |
//(--- Swift3 ---) update: 16/06/15 | |
//let r = text.startIndex.advancedBy(pos)..<text.startIndex.advancedBy(pos+1) | |
let idx1 = text.index(text.startIndex, offsetBy: pos) | |
let idx2 = text.index(text.startIndex, offsetBy: pos+1) | |
//指定範囲の文字列を取得して大文字に変換 | |
var s = text[idx1..<idx2].uppercased() | |
//大文字に変換. 変更可能にするため変数に代入. | |
var v結果 = text | |
v結果.replaceSubrange(idx1..<idx2, with: s) | |
return v結果 | |
} | |
return f指定位置を大文字にする | |
} | |
let l指定位置を大文字にする関数 = f関数を返す関数() | |
l指定位置を大文字にする関数("remove", 2) //reMove | |
l指定位置を大文字にする関数("remove", 0) //Remove | |
f関数を返す関数()("remove", 3) //remOve |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
//## 可変引数 | |
// (String...) -> ([String]) | |
// update: 16/06/15 | |
func f予定一覧(_ 予定リスト: String...) -> ([String]) { | |
var v予定配列: [String] = [] | |
for 予定 in 予定リスト { | |
v予定配列.append(予定) | |
} | |
return v予定配列 | |
} | |
print("予定一覧:\(f予定一覧("コンサート", "会食"))") | |
//結果:予定一覧:["コンサート", "会食"] | |
print("予定一覧:\(f予定一覧("支払い"))") | |
//結果:予定一覧:["支払い"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
//## ローカル引数名と外部引数名 | |
//# ローカル | |
// update: 16/06/15 | |
func f国の首都(_ key: String) -> String { | |
let l首都の辞書:[String: String] = ["日本":"東京", "米国":"ワシントン", "英国":"ロンドン"] | |
return l首都の辞書[key]! | |
} | |
print("英国の首都は、\(f国の首都("英国"))") | |
//# 外部引数名 | |
func f首都は(国名 key: String) -> String { | |
let l首都の辞書:[String: String] = ["日本":"東京", "米国":"ワシントン", "英国":"ロンドン"] | |
return l首都の辞書[key]! | |
} | |
print("英国の首都は、\(f首都は(国名:"英国"))") | |
// 外部引数名を指定しないとエラー | |
//print("英国の首都は、\(f首都は("英国"))") | |
//# タプルがパラメータ | |
var 従業員01 = ("山田花子", "部長", 200_000.0) | |
var 従業員02 = ("太田一郎", "", 150_000.0) | |
// ((String, String, Double)) -> (Doble) | |
// update: 16/06/15 | |
func f給与計算(_ 従業員: (氏名:String, 役職:String, 基本給:Double)) -> Double { | |
var 給与:Double = 従業員.基本給 | |
switch 従業員.役職 { | |
case "部長": 給与 += 50_000.0 | |
case "課長": 給与 += 30_000.0 | |
default: break; | |
} | |
return 給与 | |
} | |
print("山田花子の給与は\(f給与計算(従業員01))") | |
//結果:山田花子の給与は250000.0 | |
print("太田一郎の給与は\(f給与計算(従業員02))") | |
//結果:太田一郎の給与は150000.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
//## 引数のデフォルト値: (NSDate, String) -> (String) | |
//(--- Swift3 ---) update: 16/06/15 | |
//func f書式化した日付(日付: NSDate, 書式: String = "YY/MM/dd") -> String { | |
func f書式化した日付(_ 日付: Date, 書式: String = "YY/MM/dd") -> String { | |
//(--- Swift3 ---) update: 16/06/15 | |
//let l日付の書式 = NSDateFormatter() | |
let l日付の書式 = DateFormatter() | |
l日付の書式.dateFormat = 書式 | |
//(--- Swift3 ---) update: 16/06/15 | |
//return l日付の書式.stringFromDate(日付) | |
return l日付の書式.string(from:日付) | |
} | |
//(--- Swift3 ---) update: 16/06/15 | |
//print(f書式化した日付(NSDate())) | |
print(f書式化した日付(Date())) | |
// 結果: 16/06/15 | |
//(--- Swift3 ---) update: 16/06/15 | |
//print(f書式化した日付(NSDate(), 書式:"YYYY/MM/dd")) | |
print(f書式化した日付(Date(), 書式:"YYYY/MM/dd")) | |
// 結果: 2016/06/15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment