Concise examples of function writing in Lua


Function definition: use the keyword function to define functions, ending with the keyword end

Local variable: defined with the keyword local If it is not defined with local, even the variables defined inside the function are global variables!

A function can have multiple return values: return a, B, C

Parallel assignment: A, B = C, D

Global variables: the previous code defines three global variables: foo, R1 and R2

This is the first Lua function I wrote:


Copy codeThe code is as follows:

function foo(a,b,c)


     local sum=a+b

Return sum, C — the function can have multiple return values



R1, R2 = foo (1, ‘123’, “hello”) — parallel assignment