In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use functions in Lua". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
1. The use of functions
Example e02.lua
-- functions function pythagorean(a, b) local c2 = a^2 + b^2 return sqrt(c2) end print(pythagorean(3,4))
operation results
5
procedural notes
In Lua, functions are defined in the following format:
function name (parameter)
...
end
Unlike Pascal, end doesn't need to be paired with begin, just end after the function ends.
The function of this example is to find the hypotenuse length of a given right triangle. parameters a and b represent the length of a right angle, respectively.
A local variable is defined inside the function to store the square of the hypotenuse. Same as C, defined in function generation
Code is not executed directly, only when called by the main program.
Local means defining a local variable, if not local just means c2 is a global variable, local scope
The key is the key to the success of the project. end, while ... end etc. global variables
Scope is the entire program.
2. loop statement
e03.lua
-- Loops for i=1,5 do print("i is now " .. i) end
operation results
i is now 1
i is now 2
i is now 3
i is now 4
i is now 5
procedural notes
Here we use the for statement
for variable = parameter1, parameter2, parameter3 do
loop body
end
The variable will change from parameter 1 to parameter 2 in step 3
For example:
for i=1,f(x) do print(i) end
for i=10,1,-1 do print(i) end
print("i is now ") In i), we used.., This is used to concatenate two strings,
I don't know if you got it right.
Although i is an integer, Lua is automatically converted to string when processing, so we don't have to worry about it.
3. conditional branch statement
e04.lua
-- Loops and conditionals for i=1,5 do print("i is now " .. i) if i < 2 then print("small") elseif i < 4 then print("medium") else print("big") end end
operation results
i is now 1
small
i is now 2
medium
i is now 3
medium
i is now 4
big
i is now 5
big
procedural notes
If else usage is relatively simple, similar to C language, but here it should be noted that the whole if only needs an end,
Even if you use multiple elseif, it's an end.
for example
If op == "+" then r = a + b elseif op == "-" then r = a - b elseif op == "*" then r = a*b elseif op == "/" then r = a/b else error("invalid operation") end"How to use functions in Lua" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.