5 4 Swift函式的預設引數

2021-07-25 10:54:17 字數 1057 閱讀 4274

/**

5.4-

函式的預設引數 */

letstr1 =

joinstring

(s1:

"hell"

, tostring:

"world")

print

(str1)

letstr2 =

joinstring

(s1:

"hell"

, tostring:

"world"

, joiner:

"¥")

print

(str2)

letstr3 =

joinstring2

(s1:

"hellw"

, tostring:

"world"

, joiner:

"#")

print

(str3)

letstr4 =

joinstring2

(s1:

"llll"

, tostring:

"jijiji")

print

(str4)

/**

函式的預設引數

一定是在定義函式的時候做的

*/

// 後面那個

# 就是預設引數

, 可以是第乙個,可以出現在函式形式引數的任意位置

func

joinstring(s1:

string

, tostring s2:

string

, joiner s3:

string

= "#"

) ->

string

func

joinstring2(s1:

string

, tostring s2:

string

, joiner:

string

= "#"

) ->

string

C (帶有預設引數的函式引數)預設函式引數

預設引數?在c 中,允許實參的個數與形參的個數不同。在宣告函式原型時,為乙個或者多個形參指定預設值,以後呼叫這個函式時,若省略某乙個實參,c 則自動的以預設值作為相應引數的值。實列說明 includeusing namespace std int add int x 4,int y 7 int ma...

函式的預設引數

c 中只能定義一次預設值,即原型說明時定義了預設值,後面的函式定義不能有預設值,且預設引數都必須是從右到左定義,呼叫時實參對形參的初始化必須是從左向右的 比如 函式宣告f int i 5 定義了預設值。後面對f的函式定義時就不需要提供預設值了 f int i 即函式定義不能寫成 f int i 5 ...

函式的預設引數

c 在宣告函式原型的時候,可以為乙個或多個引數指定預設引數值,當函式呼叫的時候,如果沒有指定這個值,編譯器會自動用預設值替代。void fun int a 1,int b 2 int main 注意點 1.void fun int a,int b 1,int c 2 若形參b是預設引數,那麼形參c必...