Parameters और कई Return Values
Lua functions एक साथ कई values return कर सकते हैं और missing arguments बस nil होते हैं।
In this page:
Parameters और Multiple Return Values
एक function commas से separated किसी भी संख्या में values return कर सकता है। Extra arguments drop हो जाते हैं और missing वाले nil बन जाते हैं। जब एक call एक expression list के बीच में इस्तेमाल हो सिर्फ इसकी पहली value रखी जाती है, लेकिन एक list के अंत में एक call इसकी सारी values तक expand होती है। एक single value force करने के लिए एक call को parentheses में wrap करें।
Note:
or operator से एक default इस्तेमाल करें, जैसे name = name or "guest"।
उदाहरण: Parameters and multiple return values
local function minmax(a, b)
if a < b then return a, b else return b, a end
end
local lo, hi = minmax(9, 4)
print(lo, hi)
print(minmax(1, 2), "end")
print("start", minmax(1, 2))
print((minmax(5, 3)))
local function hello(name)
name = name or "guest"
return "hi " .. name
end
print(hello(), hello("Bo"))
-- Output:
-- 4 9
-- 1 end
-- start 1 2
-- 3
-- hi guest hi Bo
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- बहुत कम arguments pass होने पर एक error की उम्मीद करना
- list के बीच में एक call से सारी return values की उम्मीद करना
- दूसरी return value capture करना भूल जाना
चैप्टर सारांश
- Functions कई values return कर सकते हैं
- Missing arguments nil हैं
- सिर्फ list में last call expand होता है
- (f()) एक value रखता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: