1.0 函数的定义与调用( Defining and Calling Functions )

习惯了C#了语法,看到下面的这样定义输入参数实在感到非常别扭,func 有点 Javascript的感觉,还算习惯。函数调用与其他语言没什么区别

//有输入参数和返回值的函数
//输入参数为名name,数据类型为String
//返回值 String类型
func SayHello(name:String) ->String {
return "Hello,"+name;
}
//调用函数
SayHello("_luo")

2.0 函数参数与返回值 ( Function Parameters and Return Values )

// 2.1 多重输入参数 ( Multiple Input Parameters )
func HalfOpenRangeLength(start:Int,end:Int) ->Int{
return end - start;
}
// 2.2 无参函数 ( Functions Without Parameters )
func SayHelloWorld()->String{
return "Hello World";
} // 2.3 无返回值函数 ( Functions Without Return Values )
func SayGoodbye(name:String){
println("Goodbye,\(name)");
} // 2.4 多重返回值函数 (Functions with Multiple Return Values )
//你可以用元组(tuple)类型让多个值作为一个复合值从函数中返回。
//计算一个字符串中元音,辅音和其他字母的个数
func Count(str:String)->(vowels:Int,consonants:Int,others:Int){
var vowels=0,consonants=0,others=0;
for item in str {
switch String(item).lowercaseString{
case "a","e","i","o","u":vowels++;
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":consonants++;
default:others++; }
}
return (vowels,consonants,others);
}

3.0 函数参数名称 ( Function Parameter Names )

上面的函数中的参数名仅在函数体中使用,不能这函数调用时使用,这种类型的参数名称为[局部参数名]。

与C#相比,Swift 有外部参数名。

外部参数名,是为了在调用函数时,可以指出各个实参的用途是什么。

(个人觉得实在是太不人性化了,为了知道这个参数的意义还要弄个外部参数名。C#就比较好了 在vs,只要写了注释,调用的时候就会显示各个参数的用途,顿时感觉到vs的强大)

// 3.1 外部参数名 ( External Parameter Names )
//外部参数名写在局部参数名之前,用空格分隔
//如果你提供了外部参数名,那么函数在被调用时,必须使用外部参数名。
//----没有外部参数名的写法

【Swift】 - 函数(Functions)总结 - 比较 与 C# 的异同的更多相关文章

  1. asp.net MVC helper 和自定义函数@functions小结

    asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...

  2. Swift函数编程之Map、Filter、Reduce

    在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...

  3. 如何在C语言中调用Swift函数

    在Apple官方的<Using Swift with Cocoa and Objectgive-C>一书中详细地介绍了如何在Objective-C中使用Swift的类以及如何在Swift中 ...

  4. Swift 函数

    1: 函数形式: Swift函数以关键字func 标示.返回类型->后写明.如果没有返回类型可以省去.多个参数用,分割.其中参数名字在前:类型描述 func GetName(strName:St ...

  5. swift函数的用法,及其嵌套实例

    import Foundation //swift函数的使用 func sayHello(name userName:String ,age:Int)->String{ return " ...

  6. asp.net MVC 自定义@helper 和自定义函数@functions小结

    asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...

  7. [转]asp.net MVC helper 和自定义函数@functions小结

    本文转自:http://www.cnblogs.com/jiagoushi/p/3904995.html asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的 ...

  8. 【概率论】3-8:随机变量函数(Functions of a Random Variable)

    title: [概率论]3-8:随机变量函数(Functions of a Random Variable) categories: Mathematic Probability keywords: ...

  9. 【概率论】3-9:多随机变量函数(Functions of Two or More Random Variables)

    title: [概率论]3-9:多随机变量函数(Functions of Two or More Random Variables) categories: - Mathematic - Probab ...

随机推荐

  1. notepad++使用正则表达式的查找替换

    使用正则表达式可以很好地完成很多繁琐耗时的工作,以下抄录editplus正则表达式的使用,同样适用于notepad++:表达式 说明\t 制表符.\n 新行.. 匹配任意字符.| 匹配表达式左边和右边 ...

  2. spark学习11(Wordcount程序-本地测试)

    wordcount程序 文件wordcount.txt hello wujiadong hello spark hello hadoop hello python 程序示例 package wujia ...

  3. 自然语言处理中的语言模型预训练方法(ELMo、GPT和BERT)

    自然语言处理中的语言模型预训练方法(ELMo.GPT和BERT) 最近,在自然语言处理(NLP)领域中,使用语言模型预训练方法在多项NLP任务上都获得了不错的提升,广泛受到了各界的关注.就此,我将最近 ...

  4. 《Computational Statistics with Matlab》硬译2

    T=; sigma=; thetamin=-;thetamax=; theta=zeros(,T); seed=;rand('state',seed);randn('state',seed); the ...

  5. Web Service和Servlet的区别

    没接触过web service今天看了一篇文章转过来. 在最开始学习Web Service时候,总觉得Web Service和Servlet没有什么区别,觉得Servlet可以对Http请求进行相应并 ...

  6. Python类与标准库

    Python类 >>> class MyClass: ... """A simple example class""" . ...

  7. dataframe 列名重新排序

    在用list包含多个dict的模式生成dataframe时,由于dict的无序性,而uci很多数据的特征名直接是1,2,3...,生成的dataframe和原生的不一样, 为了方便观看和使用,我们将其 ...

  8. spring3: AOP 之切面实例化模型 ——跟我学spring3

    所谓切面实例化模型指何时实例化切面. Spring AOP支持AspectJ的singleton.perthis.pertarget实例化模型(目前不支持percflow.percflowbelow ...

  9. Linux常用的50个命令

    50个最常用的Unix/Linux命令 2014-08-20 这篇文章翻译自http://www.thegeekstuff.com/2010/11/50-linux-commands/这些都是一些很常 ...

  10. 使用cqlsh远程连接cassandra——设置cassandra.yaml里rpc_address和listen_address为ipv4地址即可

    You need to edit cassandra.yaml on the node you are trying to connect to and set the node ip address ...