swift函式學習

2021-07-09 13:38:18 字數 3816 閱讀 5654

//函式的定義與呼叫

//定義

func sayholle(personname : string) -> string

//呼叫

print

(sayholle(

"nick"

));

//函式引數與返回值

//多重引數

func halfopenrangelength(start : int, end : int) -> int

print

(halfopenrangelength(

3, end:

10));

//無參函式

func sayholleworld() -> string

print

(sayholleworld());

//無返回值函式

func saygoodbye(personname : string)

saygoodbye(

"nick");

//多重返回值函式

func count(string : string) -> (vowels : int, consonants : int, others : int)

}return (vowels, consonants, others);

}let

total =

count

("hclksajlhflkahflkagflsadgfliusag8whklwb");

print(

total);

//元組返回可以存放不同型別的資料

func count1(string : string) -> (vowels : int, consonants : int, others : string)

}return (vowels, consonants, others);

}let

total1 =

count1

("hclksajlhflkahflkagflsadgfliusag8whklwb");

print

(total1);

//外部引數名

/**如果希望函式的使用在呼叫函式時提供引數名字,那就需要給每個引數除了區域性引數名外再定義乙個外部引數名。外部引數名寫在區域性引數名之前,用空格分隔

*/func somefunction(jubucanshuming jubucanshuming : int)

somefunction(jubucanshuming: 10);

//可變引數

/**乙個可變引數可以接受乙個或多個值。函式呼叫時,你可以用可變引數來傳入不確定數量的輸入引數。通過在變數型別名後面加入(...)的方式定義可變引數,在函式體內可以當做陣列常量,但不是陣列.

使用時,乙個函式最多只能有乙個可變引數,而且它必須是參數列中最後乙個,這樣做是為了避免函式呼叫時出現歧義。

*///計算平均數

func arithmeticmean(numbers : double...) -> double

return total / double(numbers.count);

}print

(arithmeticmean(1,

2, 3,

4, 5));

//變數引數

func alignright(var string : string, count : int

, pad : character) -> string

return string;

}let originalstring = "hahaha";

print(alignright(originalstring, count: 10, pad: "*"));

//輸入輸出引數

/**定義乙個輸入輸出引數時,在引數定義前加inout關鍵字。乙個輸入輸出引數有傳入函式的值,這個值被函式修改,然後被傳出函式,替換原來的值。

只能傳入乙個變數作為輸入輸出引數,不能傳入常量或者字面量。當傳入的引數作為輸入輸出引數時,需要在引數前加&符,表示這個值可以被函式修改

有利於**抽取

謹防埋雷

*/func swaptwoints(inout a : int, inout b : int)

var someint = 3;

var anotherint = 10;

print

(someint,anotherint);

swap

(&someint, &anotherint);

//注意&符號

print

(someint,anotherint);

//使用函式型別

/**相同型別函式可以賦值,可以想其他型別一樣,當賦值乙個函式給常量或變數時,可以讓swift推測其函式型別

*/func addfunction(a : int, b : int) -> int

func multiplyfunction(a : int, b : int) -> int

var newaddfunction: (int, int) -> int = addfunction;

print

(newaddfunction(3,

4));

newaddfunction

= multiplyfunction;

print

(newaddfunction(2,

5));

//函式型別作為引數型別

/**函式型別可以作為另乙個函式的引數型別,可以將函式的一部分實現交由給函式的呼叫者

有利於迭代

*/func printresult(newfunction: (int, int) -> int, a : int, b : int)

printresult

(newaddfunction, a:

5, b:

10);

//函式型別作為返回型別

/**用函式型別作為另乙個函式的返回型別。需要做的是在返回箭頭後面寫乙個完整的函式型別

*/func stepforward(input : int) -> int

func stepbackward(input : int) -> int

func choosestepfunction(backward : bool) -> (int) -> int

var currentvalue = 3;

let movenearertozero = choosestepfunction(currentvalue

< 0);

currentvalue= movenearertozero(currentvalue);

print

(currentvalue);

//巢狀函式

/**可以視為區域性域函式

預設情況下,巢狀函式是對外界不可見的,但是可以被他們的封閉函式呼叫,乙個封閉函式也可以返回她的某乙個巢狀函式,使得這個函式可以在其他域中被使用

*/func choosestepfunction2(backward : bool) -> (int) -> int

func stepbackward1(input : int) -> int

return

backward ? stepbackward1: stepforward1; }

var currentvalue1 = 4;

let movenearertomax = choosestepfunction2(currentvalue1

< 0);

print

(movenearertomax(currentvalue1));

Swift2 0 函式學習筆記

最近又有點忙,忙著找工作,忙著適應這個新環境。現在好了,上班兩周周了,也適應過來了,又有時間安安靜靜的就行我們前面的學習了。今天這篇筆記,記錄的就是函式的使用。下面這些 基本上是理清楚了函式的額使用,但還有一塊 閉包 的內容,後面我們單獨寫一塊出來。形參傳遞的過程中,由於沒有寫外部形參名稱,第乙個形...

swift學習之函式

1.定義函式func sayhello personname string string sayhello iyaqi hello,iyaqi 2.引數和返回值 2.1 無引數 func sayhelloagain string sayhelloagain hello,iyaqi 2.2 無返回值 ...

swift學習之函式

當然在程式開發的時候最重要的是方法了 在swift中如何建立 使用函式 無引數函式的形式 func 方法名字 返回值 當返回值為空的時候 返回值用 void 當返回值為空的時候 void 可以省略 函式方法的建立 func sayhellow void 函式方法的呼叫 sayhellow 有引數函式...