• 通过lua自带例子学习lua 06 (2630)


    -- Example 26 -- Functions.

    -- Define a function without parameters or return value.
    function myFirstLuaFunction()
    print("My first lua function was called")
    end
    
    -- Call myFirstLuaFunction.
    myFirstLuaFunction()
    
    
    -------- Output ------
    
    My first lua function was called

    -- Example 27 -- More functions.

    -- Define a function with a return value.
    function mySecondLuaFunction()
    return "string from my second function"
    end
    
    -- Call function returning a value.
    a=mySecondLuaFunction("string")
    print(a)
    
    
    -------- Output ------
    
    string from my second function

    -- Example 28 -- More functions.

    -- Define function with multiple parameters and multiple return values.
    function myFirstLuaFunctionWithMultipleReturnValues(a,b,c)
    return a,b,c,"My first lua function with multiple return values", 1, true
    end
    
    a,b,c,d,e,f = myFirstLuaFunctionWithMultipleReturnValues(1,2,"three")
    print(a,b,c,d,e,f)
    
    
    -------- Output ------
    
    1 2 three My first lua function with multiple return values
    1 true

    -- Example 29 -- Variable scoping and functions.

    -- All variables are global in scope by default.
    
    b="global"
    
    -- To make local variables you must put the keyword 'local' in front.
    function myfunc()
    local b=" local variable"
    a="global variable"
    print(a,b)
    end
    
    myfunc()
    print(a,b)
    
    
    -------- Output ------
    
    global variable local variable
    global variable global

    -- Example 30 -- Formatted printing.

    -- An implementation of printf.
    
    function printf(fmt, ...)
    io.write(string.format(fmt, ...))
    end
    
    printf("Hello %s from %s on %s\n",
    os.getenv"USER" or "there", _VERSION, os.date())
    
    
    -------- Output ------
    
    Hello there from Lua 5.1 on 04/06/13 16:16:02
  • 相关阅读:
    Linux脚本中使用特定JDK
    redis 模糊匹配批量清理 keys
    git push 本地项目推送到远程分支
    extentreports报告插件之extentX之服务搭建(三)
    extentreports报告插件与testng集成(二)
    extentreports报告插件与testng集成(一)
    初识ios自动化(一)
    css 选择器
    appium移动端测试之滑动(二)
    使用appium进行ios测试,启动inspector时遇到的问题(一)
  • 原文地址:https://www.cnblogs.com/sdlypyzq/p/3002555.html
Copyright © 2020-2023  润新知