Swift Functional Programming(Second Edition)
上QQ阅读APP看书,第一时间看更新

Calling functions

We have covered a general syntax to define a function and method if it resides in an object. Now it is time to talk about how we call our defined functions and methods. That should not be difficult, right? There were complexities with parameters in previous versions of Swift but v3.0 solves the issues and streamlines it.

To call a function, we will use its name and provide its required parameters. For now, we are going to cover the most basic type of parameter, as follows:

funcName(firstParam: firstParamName, secondParam: secondParamName)  

To call a method, we need to use the dot notation provided by Swift. The following examples are for instance and class methods:

class AClass { 
func instanceMethod(param1: String, param2: String) {
// function body
}

class func classMethod(param1: String, param2: String) {
// function body
}
}

let aClassInstance = AClass()
aClassInstance.instanceMethod(param1: "first string", param2: "second string")
AClass.classMethod(param1: "first string", param2: "second string")