Use the Lua built-in example to learn about Lua 06 (26-30)

Source: Internet
Author: User
Tags define function

-- 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, trueenda,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 values1 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)endmyfunc()print(a,b)-------- Output ------global variable local variableglobal variable global

-- Example 30 -- formatted printing.

-- An implementation of printf.function printf(fmt, ...)io.write(string.format(fmt, ...))endprintf("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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.