zoukankan      html  css  js  c++  java
  • lua 函数

    1、函数只有一个参数,且该参数为table 或 字符串时,调用函数可以省略()

      print"hello world" 同 print("hello world")

      type{} 同 type({})

    2、函数拥有多个返回值 
    function test()
        return 1, 2, 3
    end

      2.1 多赋值中,函数为最后一个表达式,则返回所有值,否则只返回第一个值

        local x, y = test()  -- x = 1, y = 2, 3丢弃

        local x, y, z = 1, test(), 4  -- x = 1, y = 1, z = 4

      2.2 table中函数为最后一个表达式,则返回所有值,否则只返回第一个值

        t = {2, test()}  -- 2, 1, 2, 3

        t = {test(), 2}  -- 1, 2

      2.3 用()强制返回一个值

        print( test() )  -- 1, 2, 4

        print( (test()) )  -- 1

    3、不定长参数函数

      unpack()参数为一个table,默认从下标1开始返回所有元素

      lua5.1及之前,unpack为全局函数,可以直接使用,如 unpack(...)

      lua5.2后,unpack被移了到table下面,不可以直接使用,要带上table,如 table.unpack(...)

      lua5.1后 ...需要转换...后再使用,如local arg = {...}

      select("#", ...) -- 获取不定长参数的长度(包括nil)

      select(i, ...) -- 获取不定长参数中第i个位置的参数

    在孤独中思考,在思考中成熟,在成熟中升华
  • 相关阅读:
    滑动拼图
    CentOS8安装中文输入法
    windows+ubuntu 双系统时间不一致的问题
    Goland 2019下载和安装(带破解补丁和汉化包)
    防火墙站名资源脚本
    linux上以服务方式启动程序kestrel
    NLog实践记录
    sqlserver安装ubuntu
    pyspark提交集群任务
    无法打开hadoop的UI页面
  • 原文地址:https://www.cnblogs.com/laogaoyang/p/6254911.html
Copyright © 2011-2022 走看看