- Swift 4 Programming Cookbook
- Keith Moon
- 336字
- 2021-07-08 10:21:23
How to do it...
Let's look at how functions are defined in Swift:
func nameOfFunction(parameterLabel1 parameter1: ParameterType1, parameterLabel2 parameter2: ParameterType2,...) -> OutputType {
// Function's implementaion
// If the function has an output type,
// the function must return a valid value
return output
}
Let's look at this in more detail to see how a function is defined:
- func : This indicates that you are declaring a function.
- nameOfFunction : This will be the name of your function, and by convention is written in camel case (this means that each word, apart from the first, is capitalized and all spaces are removed). This should describe what the function does, and should provide some context to the value returned by the function, if one is returned. This will be how you will invoke the method from elsewhere in your code, so bear that in mind when naming it.
- parameterLabel1 parameter1: ParameterType1 : This is the first input, or parameter, into the function. You can specify as many parameters as you like, separated by commas. Each parameter has a parameter name (parameter1) and type (ParameterType1); the parameter name is how the value of the parameter will be made available to your function's implementation. You can optionally provide a parameter label in front of the parameter name (parameterLabel1 ), which will be used to label the parameter when your function is used (at the call site).
- -> OutputType : This indicates that the function returns a value and indicates the type of that value. If no value will be returned, this can be omitted. In the following code illustration, the curly brackets indicate the start and end of the function's implementation; anything within them will be executed when the function is called:
{
// Function's implementaion
}
- return output : If the function returns a value, you type return and then the value to return. This ends the execution of the function; any code written after the return statement is not executed.
Now, let's put this into action.
推薦閱讀
- MySQL 8從入門到精通(視頻教學(xué)版)
- Java高手真經(jīng)(高級編程卷):Java Web高級開發(fā)技術(shù)
- .NET 4.0面向?qū)ο缶幊搪劊夯A(chǔ)篇
- Full-Stack Vue.js 2 and Laravel 5
- Instant RubyMotion App Development
- The DevOps 2.5 Toolkit
- 基于ARM Cortex-M4F內(nèi)核的MSP432 MCU開發(fā)實踐
- Java程序員面試筆試寶典(第2版)
- 移動互聯(lián)網(wǎng)軟件開發(fā)實驗指導(dǎo)
- Visual Basic程序設(shè)計實驗指導(dǎo)及考試指南
- IBM RUP參考與認(rèn)證指南
- JavaEE架構(gòu)與程序設(shè)計
- 3ds Max瘋狂設(shè)計學(xué)院
- C語言開發(fā)入門教程
- 架構(gòu)師應(yīng)該知道的37件事