Swift # 函數

來源:互聯網
上載者:User

標籤:

  Sw i ft學習

  Swift是Apple在WWDC2014所發布的一門程式設計語言,用來撰寫OS X和iOS應用程式[1]。在設計Swift時.就有意和Objective-C共存,Objective-C是Apple作業系統在匯入Swift前使用的程式設計語言

  Swift是供iOS和OS X應用編程的新程式設計語言,基於C和Objective-C,而卻沒有C的一些相容約束。Swift採用了安全的編程模式和添加現代的功能來使得編程更加簡單、靈活和有趣。介面則基於廣受人民群眾愛戴的Cocoa和Cocoa Touch架構,展示了軟體開發的新方向。

 

================ 

 

// # 函數 func
println("[===內部和外部參數===]");
// MARK: 內部和外部參數(加#)
func say(MyName name:String, MyAge age:Int) {
   println("hello name=\"\(name)\" "+"\n\n" + " age=\"\(age)\"")
}

//say("mb", 22)
say(MyName: "mb", MyAge: 22)

// 帶#
func say(#name:String, #age:Int) {
   println("hello \(name) \(age)")
}

say(name: "mb", age: 27)

 

println("[=====交換數值=====]");
// MARK: 交換 inout (取地址,交換)
func swap(inout a: Int, inout b:Int)
{
   var temp = a;
   a = b;
   b = temp;
}

var x = 10;
var y = 20;
println("交換前 a=\(x) b=\(y)");

swap(&x, &y);
println("交換後 a=\(x) b=\(y)");

// 交換
func swap1(var a:Int, var b:Int)
{
   var temp = a;
   a = b;
   b = temp;
}

var x1 = 10;
var y2 = 20;
println("Before exchanging."+"\n\n"+" a=\(x) b=\(y)");

swap1(x1, y2);

println("After exchanging."+"\n\n"+" a=\(x) b=\(y)");

 

println("[=====交換數值=====]");

 

// 函數 變參
func add(arr:Array<Int>) -> Int{
   var sum : Int = 0;
   
   for i in arr {
       sum += i;
   }
   
   return sum;

}

println(add([1, 2, 3]));

 

func add(#a:Int, #arr:Int...) -> Int {
   var sum:Int = 0;
   
   for i in arr {
       sum += i;

   }

   return sum + a;
}

//println(add(7, [1, 2, 3]));

 

================

PS:

[ 每日一句 

" There‘s always more to learn, and there are always better ways to do what you‘ve done before. " -- Donald Ervin Knuth

 

推薦網址

http://www.cocoachina.com/ 

================  

|-> Copyright (c) 2015 Bing Ma.

|--> GitHub: https://github.com/SpongeBob-GitHub

================

Swift # 函數

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.