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

Functions को Define और Call करना

एक function code का एक named block है जिसे आप जब ज़रूरत हो चला सकते हैं।

Functions Define और Call करना

function name(params) ... end से एक function define करें और parentheses से इसे call करें। Functions values हैं, इसलिए definition एक variable को assign करने का shorthand है। local function prefer करें ताकि function global न हो। अगर एक call में एक single string या table literal argument हो parentheses omit किए जा सकते हैं।

Note: बिना return statement वाला एक function बिल्कुल कोई values return नहीं करता।

उदाहरण: Defining and calling functions

lua
local function greet(name)
  return "Hello, " .. name
end
print(greet("Lua"))
print(type(greet))
local function noReturn() end
print(noReturn())
print(#"abc", type{})

-- Output:
-- Hello, Lua
-- function
--
-- 3	table
{# 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. call करते वक्त parentheses भूल जाना और function खुद पाना
  2. accidentally globally functions define करना
  3. function के close पर end miss करना
चैप्टर सारांश
  • function name(...) end इस्तेमाल करें
  • local function prefer करें
  • parentheses से call करें
  • कोई return मतलब कोई 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.