官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 陆良县| 乌什县| 八宿县| 玉门市| 图木舒克市| 丰镇市| 车险| 连平县| 沂源县| 密云县| 两当县| 莱州市| 澄迈县| 孝义市| 安岳县| 武强县| 长汀县| 东方市| 白河县| 宁阳县| 彰化县| 大石桥市| 三门峡市| 华安县| 利川市| 南部县| 罗甸县| 鄂温| 突泉县| 同心县| 安吉县| 防城港市| 雅安市| 林芝县| 灵台县| 什邡市| 东阳市| 东乡族自治县| 崇礼县| 乌审旗| 梅州市|