当前位置:网站首页>六、Kotlin基础学习:函数
六、Kotlin基础学习:函数
2022-07-30 05:43:00 【¥伊人独醉】
1、函数的定义

MainActivity.kt



效果:

【案例2】定义排序函数

MainActivity.kt



效果:

2、函数的类型
3、单表达式函数
函数内部是一个表达式,也就是函数体非常简单,只有一句可以执行

4、函数的参数
(1)具名参数

(2)默认参数


(3)可变参数

注意:

5、函数的分类
(1)顶层函数
创建一个函数在类之外,直接在包下。

(2)成员函数
在一个类内部定义,就是成员函数

(3)局部函数
语法:
fun 函数名(){
fun 函数名(){
}
}
【案例】

6、局部函数——举例
【案例】输入一组数,求其中最大值

MainActivity.kt

效果:

7、递归函数
自己调用自己。需要设置终止条件。例如:第100步是由第99步完成后来完成。直到第1步完成,开始逐步返回。
【案例】累和
从1+。。。+100
将此问题变成:
从(1+。。。+100),变成从(1+。。。+99),再加100
从(1+。。。+99),变成从(1+。。。+98),再加99
……
从(1+2),变成返回1加上2

递归函数如果调用写在函数体最后一行,称之为尾递归函数,这样可以节约栈空间,可以提高运行效率。
8、函数的重载
函数名称一样,但是签名不一样。
【案例】

【案例】递归再举例

边栏推荐
- Trust anchor for certification path not found. Exception solution.
- awd --waf deployment
- "MySQL Advanced Chapter" four, the storage structure of the index
- 【OS】操作系统高频面试题英文版(1)
- [PASECA2019]honey_shop
- Jackson serialization failure problem - oracle data return type can't find the corresponding Serializer
- The number of warehouse 】 data quality
- Volatility memory forensics - command shows
- MySQL - 函数及约束命令
- 在不同的服务器上基于docker部署redis主从同步
猜你喜欢

使用kotlin扩展插件/依赖项简化代码(在最新版本4.0以后,此插件已被弃用,故请选择性学习,以了解为主。)
![[Net Ding Cup 2020 Qinglong Group] AreUSerialz](/img/f2/9aef8b8317eff31af2979b3a45b54c.png)
[Net Ding Cup 2020 Qinglong Group] AreUSerialz

sqli-labs shooting range SQL injection learning Less-1
Awd summary

MySQL - Multi-table query and case detailed explanation

SQL Server 数据库之生成与执行 SQL 脚本

网上说的挖矿究竟是什么? 挖矿系统开发详解介绍

线程的5种状态

Trust anchor for certification path not found. Exception solution.

Competition WP in May
随机推荐
【十年网络安全工程师整理】—100渗透测试工具使用方法介绍
MYSQL一站式学习,看完即学完
Arthas command parsing (jvm/thread/stack/heapdump)
[Ten years of network security engineers finishing] - 100 penetration testing tools introduction
十七、Kotlin进阶学习:1、守护线程;2、线程和协程之间的效率对比;3、取消协程;
标准输入输出流(System.in,System.out)
The Request request body is repackaged to solve the problem that the request body can only be obtained once
Bypassing the file upload vulnerability
sqli-labs less3/4 Targeting Notes
TDengine集群搭建
C#中default关键字用法简介
MySQL 索引优化及失效场景
使用PyQt5为YoloV5添加界面(一)
Use kotlin to extend plugins/dependencies to simplify code (after the latest version 4.0, this plugin has been deprecated, so please choose to learn, mainly to understand.)
Servlet基本原理与常见API方法的应用
Servlet basic principles and application of common API methods
C#下大批量一键空投实现
Flink CDC 实现Postgres到MySQL流式加工传输案例
C#利用开源NPlot实现K线图(蜡烛图)
MySQL 特殊语句及优化器