Swift - Function Parameter Default Value

Introduction

You can create functions whose parameters have default values.

You can call these functions and omit certain parameters.

Those parameters will use the values provided in the function's definition:

Demo

func multiplyNumbers2(firstNumber: Int, multiplier: Int = 2) -> Int { 
    return firstNumber * multiplier; 
} 
// Parameters with default values can be omitted 
var a = multiplyNumbers2(firstNumber: 2) // 4 
print(a)//from  w ww  .  j a  v a  2s  .c  o m

Result

Related Topic