← Back to Lua Course | Chapter 5: Functions | Lesson 2 of 7

Parameters और कई Return Values

Lua functions एक साथ कई values return कर सकते हैं और missing arguments बस nil होते हैं।

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

lua
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
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बहुत कम arguments pass होने पर एक error की उम्मीद करना
  2. list के बीच में एक call से सारी return values की उम्मीद करना
  3. दूसरी return value capture करना भूल जाना
चैप्टर सारांश
  • Functions कई values return कर सकते हैं
  • Missing arguments nil हैं
  • सिर्फ list में last call expand होता है
  • (f()) एक value रखता है

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.