본문 바로가기
PROGRAMMING CODE/SWIFT

[Swift] 기초 예제 2 (함수연습)

by daye_ 2023. 5. 31.

 

//code1
func callName (_ name : String) {
    print("\(name)님 환영합니다!")
}
callName("jdh")
 
 
 //code2
 
 func callName (_ name : String) {
      print("\(name)님 환영합니다!")
  }

  let name = ["jdh","lhh","yyk","jjh","lky"]

 for i in 0..<name.count {
      callName(name[i])
  }

 
 //code3-1
 
 func callName (_ num : Int)-> Int {
     return num
 }

 let name = ["jdh","lhh","yyk","jjh","lky"]

 print("\(name[callName(3)])님 환영합니다!")
 
 
  //code3-2
 
 func callName (_ name : [String], _ num : Int) {
      print("\(name[num])님 환영합니다!")
  }

  let name = ["jdh","lhh","yyk","jjh","lky"]

 callName(name,3)
  
 
 //code4
 
 func callName (_ name : [String], _ num : Int)-> String {
       return name[num]
   }

   let name = ["jdh","lhh","yyk","jjh","lky"]

  print("\(callName(name,3))님 환영합니다!") 
 
 
 //code5
 
 func callName (_ name : [String], _ num : Int)-> (memberName:String,index:Int) {
        return (name[num],num)
    }

    let name = ["jdh","lhh","yyk","jjh","lky"]

 var myTuple : (memberName :String, index:Int)  = callName(name,3)

 print("\(myTuple.memberName)님 환영합니다!") 
 
 
 //code6
 
 func callName (_ name : String...) {
     for i in name {
         print("\(i)님 환영합니다!")
     }
  }

  let name = ["jdh","lhh","yyk","jjh","lky"]

 callName(name[0],name[1],name[2],name[3],name[4])