swift基本语法(总结提炼版)之010 swift 之函数
来源:互联网 发布:为什么玩lol网络不稳定 编辑:程序博客网 时间:2024/05/22 04:55
函数
简单演练
func sum(a: Int, b: Int) -> Int { return a + b}
- 阶段性小结
- 函数定义格式:
func 函数名(参数: 参数类型...) -> 返回值 { // 代码实现 }
- 如果没有返回值,
-> 返回值
可以省略 ->
是一个很有意思的符号- 默认情况下,在调用函数时,第一个参数名是省略的
- 函数定义格式:
参数名的特殊处理
强制要求参数名
func sum1(x a: Int, y b: Int) -> Int { return a + b}
省略参数名
func sum2(a: Int, _ b: Int) -> Int { return a + b}
操练:
/*
函数: 完成某个特定任务的代码块, 给代码起一个合适的名称称之为函数名称. 以后需要执行代码块只需要利用函数名称调用即可, 好比每个人都有一个名字, 叫名字就能找到对应的人格式:
func 函数名称(参数名:参数类型, 参数名:参数类型...) -> 函数返回值 {函数实现部分}
OC:
- (void)say
{
NSLog(@"hello");
}
- (void)sayWithName:(NSString *)name
{
NSLog(@"hello %@", name);
}
- (void)sayWithName:(NSString *)name age:(NSInteger)age
{
NSLog(@"hello %@ , I'm %tu years old", name, age);
}
- (NSString *)info
{
return @"name = lnj, age = 30";
}
- (NSString *)infoWithName:(NSString *)name age:(NSInteger)age
{
return [NSString stringWithFormat:@"name = %@, age = %tu", name, age];
}
Person *p = [[Person alloc] init];
[p say];
[p sayWithName:@"lnj"];
[p sayWithName:@"lnj" age:30];
NSLog(@"%@", [p info]);
NSLog(@"%@", [p infoWithName:@"lnj" age:30]);
*/
// 无参无返回值
func say() -> Void
{
print("hello")
}
func say1() // 如果没有返回值可以不写
{
print("hello")
}
say1()
// 有参无返回值
func sayWithName(name:String)
{
print("hello \(name)")
}
sayWithName("lnj")
func sayWithName1(name:String, age:Int)
{
print("hello \(name) , I'm \(age) years old ")
}
sayWithName1("lnj", age: 30)
// 无参有返回值
func info() -> String
{
return "name = lnj, age = 30"
}
print(info())
// 有参有返回值
func info(name:String, age:Int) -> String
{
return "name = \(name), age = \(age)"
}
print(info("lnj", age:30))
/*
嵌套函数
*/
func showArray(array:[Int])
{
for number in array
{
print("\(number), ")
}
}
/*
func swap(inout a:Int, inout b:Int)
{
let temp = a
a = b
b = temp
}
*/
var number = 998;
func bubbleSort(inout array:[Int])
{
print(number)
func swap(inout a:Int, inout b:Int)
{
print(number)
let temp = a
a = b
b = temp
}
let count = array.count;
for var i = 1; i < count; i++
{
for var j = 0; j < (count - i); j++
{
if array[j] > array[j + 1]
{
swap(&array[j], b: &array[j + 1])
// let temp = array[j]
// array[j] = array[j + 1]
// array[j + 1] = temp
}
}
}
}
var arr:Array<Int> = [31, 13, 52, 84, 5]
bubbleSort(&arr)
showArray(arr)
0 0
- swift基本语法(总结提炼版)之010 swift 之函数
- swift基本语法(总结提炼版)之011 swift 之函数类型
- swift基本语法(总结提炼版)之004 Swift之 for 循环
- swift基本语法(总结提炼版)之005 Swift之 switch循环
- swift基本语法(总结提炼版)之006 Swift之 while循环
- swift基本语法(总结提炼版)之007 swift之数组
- swift基本语法(总结提炼版)之008 swift 之字典
- swift基本语法(总结提炼版)之009 swift 之字符串
- swift基本语法(总结提炼版)之012 swift 之闭包
- swift基本语法(总结提炼版)之013 swift 之闭包返回值
- swift基本语法(总结提炼版)之014 swift 之闭包循环引用
- swift基本语法(总结提炼版)之009 swift 之字符串
- swift基本语法(总结提炼版)之003 Swift类型转换
- swift基本语法(总结提炼版)之001常量&变量
- swift基本语法(总结提炼版)之002逻辑分支
- Swift学习之基本语法
- iOS开发之Swift基本语法详解
- [swift学习之二]基本语法练习
- 【LeetCode】268. Missing Number
- poj 3280 Cheapest Palindrome
- java中遍历Map的四种方式
- UVA 297(p160)----Quadtress
- An iterative image registration technique with an application to stereo vision笔记
- swift基本语法(总结提炼版)之010 swift 之函数
- 2015年我走过的日子
- js中trim的实现
- 项目检出JRE问题(Unbound classpath container: 'JRE System Library [JavaSE-1.7]' in project 'idweb')
- UVA 400(p127)----Unix Is
- spring的学习小结6_bean的生命周期
- linux中fork()函数详解
- [20160225] 太空工程师入门 / LOL 10级 / 股票走势预测
- UVA 401(p48)----Palindromes