function的練習,孤單。
func這個指令可將許多具有規則重複的指令變成一個函數,蠻有模組化的概念,還是它就是? 要用一首歌的歌詞來思考練習func的指令,於是就選用這首知名的DJ歌曲吧。
Alan Walker Alone
歌詞:
Lost in your mind
I wanna know
Am I losing my mind?
Never let me goIf this night is not forever
At least we are together
I know I’m not alone
I know I’m not alone
Anywhere, whenever
Apart, but still together
I know I’m not alone
I know I’m not aloneI know I’m not alone
I know I’m not aloneUnconscious mind
I’m wide awake
Wanna feel one last time
Take my pain awayIf this night is not forever
At least we are together
I know I’m not alone
I know I’m not alone
Anywhere, whenever
Apart, but still together
I know I’m not alone
I know I’m not alone
以func的概念,改編如下:
func notalone1 () {
print( “I know I’m not alone” )
print( “I know I’m not alone” )
}
func notalone2 () {
print( “I know I’m not alone” )
print( “I know I’m not alone” )
print( “I know I’m not alone” )
print( “I know I’m not alone” )
}
func part1 () {
print( “Lost in your mind” )
print( “I wanna know” )
print( “Am I losing my mind?” )
print( “Never let me go” )
print( “If this night is not forever” )
print( “At least we are together” )
}
func part2 () {
print( “Anywhere, whenever” )
print( “Apart, but still together” )
}
func part3 () {
print( “Unconscious mind” )
print( “I’m wide awake” )
print( “Wanna feel one last time” )
print( “Take my pain away” )
print( “If this night is not forever” )
print( “At least we are together” )
}
func part4 () {
print( “Anywhere, whenever” )
print( “Apart, but still together” )
}
part1 ()
notalone1 ()
part2 ()
notalone2 ()
part3 ()
notalone1 ()
part4 ()
notalone2 ()
然後也確定執行無誤。