Swift 函数的基本使用
来源:互联网 发布:sql复合主键3个 编辑:程序博客网 时间:2024/06/05 14:12
//
// main.swift
// testFunctions
//
// Created by 朱立志 on 14-6-11.
// Copyright (c) 2014年 zlz. All rights reserved.
//
import Foundation
println("Hello, World!")
//单个参数输入函数
func sayHello(personName : String ) -> String{
return "hello func "+personName+"!"
}
println(sayHello("zlz"))
// 多个参数传入
func halfOpenRangeLength(start : Int , end : Int ) -> Int{
return start + end
}
println(halfOpenRangeLength(2,4))
// 没有返回值的函数
func sayGoodBye(personName :String ) {
println("good bye "+personName)
}
sayGoodBye("zlz")
// 多个返回值函数
func count(inputStr : String) ->(vowels : Int , consonants : Int , others : Int){
var vowels = 0 , consonants = 0, others = 0
for charset in inputStr {
switch String(charset).lowercaseString {
case "a","e","i","o":
++vowels
case "b","c","d","f","l","k":
++consonants
default :
others++
}
}
return (vowels , consonants , others)
}
println(count("abdefiolkzwn"))
// 参数别名 #表示函数参数名和函数内部参数名 // “local parameter name and the external parameter name”
func containsCharacter(#string: String, #characterToFind: Character) ->Bool {
for character in string {
if character == characterToFind {
return true
}
}
return false
}
// 给函数参数定义默认值
func join (string s1: String , toString s2 : String , withJoiner joiner :String = "") ->String{
return s1+s2+joiner
}
println(join(string :"zhu",toString: " lizhi ",withJoiner:" joiner"))
// 可变参数
func arithmeticMean (numbers : Double ...) -> Double {
var total:Double =0
for number in numbers {
total += number
}
return total
}
println(arithmeticMean(3,2,5,7,9,13))
func addTwoInts(a : Int , b : Int)-> Int{
return a+b
}
var mathFunction : (Int ,Int) ->Int = addTwoInts
println(mathFunction(3,2))
// 函数参数是函数的函数
func printMathResult(mathFunction:(Int,Int) ->Int , a : Int , b :Int){
println("result\(mathFunction(a,b))")
}
println(printMathResult(mathFunction ,4 , 5))
- Swift 函数的基本使用
- Swift的基本使用(2)函数的定义
- Swift学习记录 -- 11.函数的基本使用
- Swift-GCD的基本使用(Swift 3.0)
- Swift函数的使用
- Swift-UITableView的基本使用
- swift UIScrollview 的基本使用
- Swift 函数的各种使用
- swift的基本使用(3)嵌套函数和闭包
- Swift 函数的基本写法与使用元组实现返回多个值(四)
- swift -基本数据类型的定义与使用
- Swift的一些基本使用(1)
- iOS --- Swift中泛型(Generic)的基本使用
- Swift学习之--TableView的基本使用
- Swift对字典的常用基本使用
- swift 画图 Charts的基本使用
- Swift基本使用-函数和闭包(三)
- 基本函数的使用
- 黑马程序员——————————面向对象3
- vector 容器
- spring事务管理的几个问题
- HDU--3033 I love sneakers!
- C语言---qsort函数
- Swift 函数的基本使用
- php中include和require的用法
- 项目构建之maven篇:2.HelloWorld项目构建过程
- listview设置不同选项的点击效果
- 黑马程序员--交通灯管理系统
- 搜索引擎(1)Spider抓取网页
- linux安装系统后增加物理内存swap
- a href=javascript:void(0) 用法
- 【Linux驱动】TQ2440 LED驱动程序