[예제]
function add_(a)
local sum = 0
for i = 1, #a do
sum = sum + a[i]
end
return sum
end
function f(a, b) print(a, b) end
f(3)
f(3, 4)
f(3, 4, 5)
count = 0
function incCount(n)
n = n or 1 --default of n is 1
count = count + n
end
--#mutiple return
s, e = string.find("hello Lua users", "Lua")
print(s, e) --return start point, end point
function maximum(a)
local mi = 1 -- index of maximum value
local m = a[mi] -- maximum value
for i = 1, #a do
if a[i] > m then
mi = i; m = a[i]
end
end
return m, mi
end
print(maximum({8,10,23,12,5}))
--다형성 Polymorphism
function foo0() end
function foo1() return "a" end
function foo2() return "a", "b" end
print(foo2(), 1)
print(foo2() .. "x")
t = {foo0()} -- t = {}
t = {foo1()} -- t = {"a"}
t = {foo2()} -- t = {"a","b"}
t = {foo0(), foo2(), 4} -- t = {nil, "a", 4}
function foo(i)
if i == 0 then return foo0()
elseif i == 1 then return foo1()
elseif i == 2 then return foo2()
end
end
print(foo(0))
print(foo(1))
print(foo(2))
print(foo(3))
print((foo0()))
print((foo1()))
print((foo2()))
--caution : foo2() != (foo2()) foo2() -> "a", "b" (foo2()) -> "a"
--CAUTION : before LUA 5.1 -> unpack, after LUA 5.2 -> table.unpack
print(table.unpack{10,20,30})
a, b = table.unpack{10,20,30}
--Recursion
function unpack(t, i, n)
i = i or 1
n = n or #t
if i <= n then
return t[i], unpack(t, i+1, n)
end
end
function add(...) -- varang expression
local s = 0
for i, v in ipairs{...} do -- (...) means variable factor
s = s + v
end
return s
end
print(add(3, 4, 10, 25, 12))
function id(...)
return ...
end --return all factor
function foo10(...)
print("calling foo:", ...)
return foo(...)
end
function fwrite(fmt, ...)
return io.write(string.format(fmt,...))
end
-- table.pack is valid after LUA 5.2
-- using table.pack, finding nil
function nonils(...)
local arg = table.pack(...)
for i = 1, arg.n do
if arg[i] == nil then return false end
end
return true
end --if table has nil, return false. if not, return true.
print(nonils(2, 3, nil))
print(nonils(2, 3))
print(nonils())
print(nonils(nil))
function rename(arg)
return os.rename(arg.old,arg.new)
end
--named parameter
function Window(options)
--below is mendatory
if type(options.title) ~= "string" then
error("no title")
elseif type(options.width) ~= "number" then
error("no width")
elseif type(options.height) ~= "number" then
error("no height")
end
--below is not mendatory
_Window(options.title,
options.x or 0, --default is 0
options.y or 0, --default is 0
options.width, options.height,
options.bacground or "white", --default is white
options.border --default is nil
)
end
--w = Window{x=0, y=0, width=300, height=200, title="Lua", background="blue", border = true}
[결과]
3 nil
3 4
3 4
7 9
23 3
a 1
ax
a
a b
nil
a
a
10 20 30
54
false
true
true
false
function add_(a)
local sum = 0
for i = 1, #a do
sum = sum + a[i]
end
return sum
end
function f(a, b) print(a, b) end
f(3)
f(3, 4)
f(3, 4, 5)
count = 0
function incCount(n)
n = n or 1 --default of n is 1
count = count + n
end
--#mutiple return
s, e = string.find("hello Lua users", "Lua")
print(s, e) --return start point, end point
function maximum(a)
local mi = 1 -- index of maximum value
local m = a[mi] -- maximum value
for i = 1, #a do
if a[i] > m then
mi = i; m = a[i]
end
end
return m, mi
end
print(maximum({8,10,23,12,5}))
--다형성 Polymorphism
function foo0() end
function foo1() return "a" end
function foo2() return "a", "b" end
print(foo2(), 1)
print(foo2() .. "x")
t = {foo0()} -- t = {}
t = {foo1()} -- t = {"a"}
t = {foo2()} -- t = {"a","b"}
t = {foo0(), foo2(), 4} -- t = {nil, "a", 4}
function foo(i)
if i == 0 then return foo0()
elseif i == 1 then return foo1()
elseif i == 2 then return foo2()
end
end
print(foo(0))
print(foo(1))
print(foo(2))
print(foo(3))
print((foo0()))
print((foo1()))
print((foo2()))
--caution : foo2() != (foo2()) foo2() -> "a", "b" (foo2()) -> "a"
--CAUTION : before LUA 5.1 -> unpack, after LUA 5.2 -> table.unpack
print(table.unpack{10,20,30})
a, b = table.unpack{10,20,30}
--Recursion
function unpack(t, i, n)
i = i or 1
n = n or #t
if i <= n then
return t[i], unpack(t, i+1, n)
end
end
function add(...) -- varang expression
local s = 0
for i, v in ipairs{...} do -- (...) means variable factor
s = s + v
end
return s
end
print(add(3, 4, 10, 25, 12))
function id(...)
return ...
end --return all factor
function foo10(...)
print("calling foo:", ...)
return foo(...)
end
function fwrite(fmt, ...)
return io.write(string.format(fmt,...))
end
-- table.pack is valid after LUA 5.2
-- using table.pack, finding nil
function nonils(...)
local arg = table.pack(...)
for i = 1, arg.n do
if arg[i] == nil then return false end
end
return true
end --if table has nil, return false. if not, return true.
print(nonils(2, 3, nil))
print(nonils(2, 3))
print(nonils())
print(nonils(nil))
function rename(arg)
return os.rename(arg.old,arg.new)
end
--named parameter
function Window(options)
--below is mendatory
if type(options.title) ~= "string" then
error("no title")
elseif type(options.width) ~= "number" then
error("no width")
elseif type(options.height) ~= "number" then
error("no height")
end
--below is not mendatory
_Window(options.title,
options.x or 0, --default is 0
options.y or 0, --default is 0
options.width, options.height,
options.bacground or "white", --default is white
options.border --default is nil
)
end
--w = Window{x=0, y=0, width=300, height=200, title="Lua", background="blue", border = true}
[결과]
3 nil
3 4
3 4
7 9
23 3
a 1
ax
a
a b
nil
a
a
10 20 30
54
false
true
true
false
[LUA] Chap 5. Function
Reviewed by kukanuc
on
3월 10, 2019
Rating:
댓글 없음: