函数: 完成某个特定任务的代码块, 给代码起一个合适的名称称之为函数名称. 以后需要执行代码块只需要利用函数名称调用即可, 好比每个人都有一个名字, 叫名字就能找到对应的人
格式:
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)
函数类型:
类似于C语言的指向函数的指针
类似于OC语言的block
函数类型是由函数的参数类型和返回值类型组成的
// 这两个函数的类型是 (Int, Int) -> Int
func sum(a: Int, b: Int) ->Int
{
return a + b;
}
func sub(a: Int, b: Int) ->Int
{
return a - b;
}
// 可以利用函数类型定义函数变量和常量
var funcP:(Int, Int) -> Int = sum
funcP = sub
print(funcP(10, 20))
// 函数类型可以作为函数的参数
func calFunc(a: Int, b: Int ,Option:(Int, Int) -> Int) ->Int
{
return Option(a, b)
}
print(calFunc(10, b: 20, Option: sum))
print(calFunc(10, b: 20, Option: sub))
// 函数类型可以作为函数返回值
func max(a: Int, b: Int) -> Int
{
return a > b ? a : b
}
func min(a: Int, b: Int) -> Int
{
return a < b ? a : b
}
func chooseFunc(getMax:Bool) -> (Int , Int) -> Int
{
return getMax ? max : min
}
var funcP2:(Int , Int) -> Int = chooseFunc(false)
print(funcP2(10, 20))