首页 > 其他分享 >iOS开发Swift-函数

iOS开发Swift-函数

时间:2023-08-25 11:45:20浏览次数:146  
标签:return 函数 Int iOS person func Swift String

1.函数的定义和调用

func greet(person: String) -> String {
//    函数名   传入值   传入值类型  返回值类型
    let greeting = "Hello" + person
    return greeting
}
print( greet(person: "Anna") )   //调用

2.函数的参数与返回值

 (1)无参函数

func sayHello() -> String {
    return "hello!"
}
print( sayHello() )

(2)多参函数

func greet(person: String, alreadyGreeted: Bool) -> String {
    if alreadyGreeted {
        return greetAgain(person: person)
    }else {
        return greet(personn: person)
    }
}

(3)无返回值

func greet(person: String) {
    print("Hello, \(person)")
}
greet(person: "Dave")

(4)多重返回值

func minMax(array: [Int]) -> (min: Int, max: Int) {
    //业务代码
    return (a, b)
}

(5)可选元组返回类型(元组可以是nil)

func minMax(array: [Int]) -> (min: Int, max: Int)? {
    //业务代码
    return (a, b)
}

(6)隐式返回的函数

func greeting(for person: String) -> String {
    "Hello " + person
}
print(greeting(for: "Dave")

任一可以被写成一行return的函数,return(x) + for。

调用的时候: 方法名(for: 参数)

3.参数标签和参数名称

(1)指定参数标签

func greet(from hometown: String) -> String {
    return "from \(hometown)."
}
print( greet(from: "Beijing") )

(2)忽略参数标签

func some(_ a: Int, b: Int) {
    //代码
}
som(1, b: 2)

(3)默认参数值

func some(a: Int, b: Int = 12) {
    //代码
}
some(a: 3, b: 6)   //b用6
some(a: 3)   //b用12

(4)可变参数

一个可变参数可接受0个或多个值。

func arith(_ numbers: Double ...) -> {
    //代码
}
arith(1, 2, 3, 4, 5)

(5)输入输出参数(&)

函数参数默认为常量,不可修改。如果要修改,则要把参数设置为输入输出参数。

func swap(_a: inout Int, _b: inout Int) {
    //代码
}
swap(&5, &7)

4.函数类型

//类型: (Int, Int) -> Int
func add(_ a: Int, _ b: Int) -> Int {
  return a + b
}
//类型: () -> Void
func printHello() {
    print("H")
}

 (1)使用函数类型

var 变量: (Int, Int) -> Int = add
变量(2, 3)    //调用

(2)函数类型作为参数类型

func printMath(_ mathFunction: (Int, Int) -> Int, a: Int, b: Int) {
    print( mathFunction(a, b) )
}
printMath(add, 3, 5)

(3)函数类型作为返回类型

func choose(back: Bool) -> (Int) -> Int{
    return add
}
let move = choose(back: true)

5.嵌套函数

把函数定义到别的函数体中,对外界不可见,但可以被他们的外围函数调用。

func addMul(a: Int, b: Int, c: Int) -> Int {
    func add(d: Int, e: Int) -> Int { return d + e }
    func mul(f: Int, g: Int) -> Int { return f * g }
    return mul(a, add(b, c))
}
print(addMul(1, 2, 3))

 


 

标签:return,函数,Int,iOS,person,func,Swift,String
From: https://www.cnblogs.com/lysboke/p/17656520.html

相关文章

  • 1 输出函数:print()
    1输出字符print('HelloWorld!')2输出表达式print(1+1)3输出到文件fp=open('D:\Text.txt','a+');#以读写的方式打开text.txt,文件不存在则新建;存在就在内容后追加print('HelloWorld!',file=fp)fp.close......
  • public async void Start(){ await 函数 } 相当于是同步方法吗?
    在C#中,使用`async`和`await`关键字可以创建异步方法。异步方法不会阻塞当前线程,允许程序在等待耗时操作的同时继续执行其他任务。在你的代码中,`publicasyncvoidStart()`是一个异步方法的声明。然而,与同步方法不同,`await`关键字会将控制权返回给调用方,允许其他操作继续......
  • DQL-聚合函数
       ......
  • js_使用axios请求图片资源, 并读取图片资源为base64格式
    情景再现:今天在写页面时遇到这么一个请求:有一张图片,默认隐藏,要求在该图片加载完毕后,执行取消隐藏的动画.目的是不要在执行动画期间图片有空白的样子.第一个想到的当然是img的onload回调函数.但是天生反骨不爱用行内元素.于是想到使用ajax请求图片资源,再使用FileR......
  • 深度学习(十三)——损失函数与反向传播
    一、损失函数:LossFunction官网文档:torch.nn—PyTorch2.0documentation1.LossFunction的作用每次训练神经网络的时候都会有一个目标,也会有一个输出。目标和输出之间的误差,就是用\(Loss\)\(Function\)来衡量的。所以,误差\(Loss\)是越小越好的。此外,我们可以根据误......
  • C++拷贝构造、赋值函数
    拷贝构造拷贝构造就是一种特殊版本的构造函数,格式:类名(const类名&that){    //执行给每个成员变量进行赋值  }什么时候会调用拷贝构造:当使用旧对象(已new的)给新对象(新new的)初始化时,会自动调用拷贝构造    Testt1;//调用无参构造Testt2=t1......
  • C++this指针、常函数
    this指针this指针的类型:类类型*const。不能被修改和赋值。只能在成员函数的内部使用。全局函数、静态函数都不能使用this.this指针本质上其实是一个成员函数的形参(栈),是对象调用成员函数时,将对象地址作为实参传递给this形参。所以对象中不存储this指针。this指针是成......
  • 每天一个小知识,今日知识-如何设计一个并发请求控制函数
    假如给你一个数组,里面是请求路径,如何设计一个函数去控制这些请求的并发呢?这里我们用的请求路径为https://jsonplaceholder.typicode.com/todos来模拟constreqArr=[];for(leti=1;i<=10;i++){reqArr.push(`https://jsonplaceholder.typicode.c......
  • 一些学习网站和自己写的两个计算周的函数
    toad:https://blog.csdn.net/zzpl139/article/details/127553557风控指标:https://blog.csdn.net/eroswang/article/details/117735703vintage:https://zhuanlan.zhihu.com/p/163206686风控模型:https://falbang.com/?p=350天池:https://tianchi.aliyun.com/competition/entrance/53183......
  • iOS开发Swift-控制流
    1.For-In循环//集合循环letnames=["a","b","c"]fornameinnames{print("Hello,\(name)!")}//次数循环forindexin1...5{print("Hello!+\(index)")}//不需要值时可以使用_来忽略此值for_in1...5{pri......