Swift functions with external parameters

详细定义

Function Parameter Names

Function parameters have both an external parameter name and a local parameter name. An external parameter name is used to label arguments passed to a function call. A local parameter name is used in the implementation of the function.

  1. func someFunction(firstParameterName: Int, secondParameterName: Int) {
  2. // function body goes here
  3. // firstParameterName and secondParameterName refer to
  4. // the argument values for the first and second parameters
  5. }
  6. someFunction(1, secondParameterName: 2)

By default, the first parameter omits its external name, and the second and subsequent parameters use their local name as their external name. All parameters must have unique local names, but may share external parameter in common.

Specifying External Parameter Names

You write an external parameter name before the local parameter name it supports, separated by a space:

  1. func someFunction(externalParameterName localParameterName: Int) {
  2. // function body goes here, and can use localParameterName
  3. // to refer to the argument value for that parameter
  4. }

NOTE

If you provide an external parameter name for a parameter, that external name must always be used when you call the function.

Here’s a version of the sayHello(_:) function that takes the names of two people and returns a greeting for both of them:

  1. func sayHello(to person: String, and anotherPerson: String) -> String {
  2. return "Hello \(person) and \(anotherPerson)!"
  3. }
  4. print(sayHello(to: "Bill", and: "Ted"))
  5. // prints "Hello Bill and Ted!"

By specifying external parameter names for both parameters, both the first and second arguments to thesayHello(to:and:) function must be labeled when you call it.

The use of external parameter names can allow a function to be called in an expressive, sentence-like manner, while still providing a function body that is readable and clear in intent.

Omitting External Parameter Names

If you do not want to use an external name for the second or subsequent parameters of a function, write an underscore (_) instead of an explicit external name for that parameter.

  1. func someFunction(firstParameterName: Int, _ secondParameterName: Int) {
  2. // function body goes here
  3. // firstParameterName and secondParameterName refer to
  4. // the argument values for the first and second parameters
  5. }
  6. someFunction(1, 2)

NOTE

Because the first parameter omits its external parameter name by default, explicitly writing an underscore is extraneous.

Default Parameter Values

You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter’s type. If a default value is defined, you can omit that parameter when calling the function.

  1. func someFunction(parameterWithDefault: Int = 12) {
  2. // function body goes here
  3. // if no arguments are passed to the function call,
  4. // value of parameterWithDefault is 42
  5. }
  6. someFunction(6) // parameterWithDefault is 6
  7. someFunction() // parameterWithDefault is 12

NOTE

Place parameters with default values at the end of a function’s parameter list. This ensures that all calls to the function use the same order for their nondefault arguments, and makes it clear that the same function is being called in each case.

时间: 2024-10-25 23:19:53

Swift functions with external parameters的相关文章

Calling Functions With Pointer Parameters

? 参数类型是Constant Pointer 也就是 UnsafePointer<Type> 可以传入的类型: UnsafePointer<Type>/UnsafeMutablePointer<Type>/AutoreleasingUnsafeMutablePointer<Type> String.如果Type 是 UInt8 或 Int8. 可变类型的 Type 的 in-out 类型. [Type] 类型,被当作指向第一个元素的地址 例子如下: fun

Swift: missing argument label &#39;xxx&#39; in call

今天在使用swift时发现,写的func总是要求写出第二个参数的外部变量名,很不理解,感觉和书上说的function不一样,查了一下,终于发现了原因:写在class内部的function叫做method,是特殊的functoin,系统会自动补上外部变量名,参看以下连接 http://stackoverflow.com/questions/24050844/swift-missing-argument-label-xxx-in-call 防止连接失效,截取部分内容如下: One possible

[Swift]Day06:函数

函数 参数 外部变量名 一般情况下你可以不指定外部变量名,直接调用函数: func helloWithName(name: String, age: Int, location: String) { println("Hello \(name). I live in \(location) too. When is your \(age + 1)th birthday?") } helloWithName("Mr. Roboto", 5, "San Fra

Swift学习—教程学习五 函数(function)

5 函数(Functions) 函数是用来完成特定任务的独立的代码块. 5.1 定义与调用Defining and Calling Functions 函数名(参数列表)->返回值 { 函数体(要干什么) } 函数名用来描述其要完成的任务,调用函数时要向函数传递其要求的输入参数,参数顺序必须与函数参数列表一致. func greet(person: String) -> String { let greeting = "Hello, " + person + "!

进击的雨燕-----------函数

函数是用来完成特定任务的独立的代码块.你给一个函数起一个合适的名字,用来标识函数做什么,并且当函数需要执行的时候,这个名字会被用于“调用”函数. Swift 统一的函数语法足够灵活,可以用来表示任何函数,包括从最简单的没有参数名字的 C 风格函数,到复杂的带局部和外部参数名的 Objective-C 风格函数.参数可以提供默认值,以简化函数调用.参数也可以既当做传入参数,也当做传出参数,也就是说,一旦函数执行结束,传入的参数值可以被修改. 在 Swift 中,每个函数都有一种类型,包括函数的参数

Swift-2.6函数

本页包含内容: 函数定义与调用(Defining and Calling Functions) 函数参数与返回值(Function Parameters and Return Values) 函数参数名称(Function Parameter Names) 函数类型(Function Types) 嵌套函数(Nested Functions) 函数是用来完成特定任务的独立的代码块.你给一个函数起一个合适的名字,用来标识函数做什么,并且当函数需要执行的时候,这个名字会被用于“调用”函数. Swif

iOS -- warnings

Semantic Warnings Warning Message -WCFString-literal input conversion stopped due to an input byte that does not belong to the input codeset UTF-8 -WNSObject-attribute         __attribute ((NSObject)) may be put on a typedef only, attribute is ignore

IOS 警告 收集

Semantic Warnings Warning Message -WCFString-literal input conversion stopped due to an input byte that does not belong to the input codeset UTF-8 -WNSObject-attribute __attribute ((NSObject)) may be put on a typedef only, attribute is ignored -Wabst

V8Sharp的中文乱码问题解决

V8是一个开源的javascript引擎,到现在为止堪称为是性能最好最稳定的javascript.因此还诞生了一个基于此引擎的服务端开发框架:Node.js.由此可见此引擎的牛逼之处.由于打算在后续项目中使用javascript,从而使项目的变化部分变更为动态配置的. 基于C#和V8的javascript引擎有很多.但到目前为止都存在一些问题,以下将分别说明: javascript.net: 项目地址:http://javascriptdotnet.codeplex.com/ 特点: 1 使用简