← Back to Bash Course | Chapter 5: Functions | Lesson 2 of 11

Functions Define और Call करना

एक function commands का एक named block है जो आप एक बार define करते हैं और फिर बस इसका नाम लिखकर बार-बार चला सकते हैं, एक mini command को उसका अपना नाम देने जैसा।
Syntax
bash
function_name() {
  commands
}

function_name

Defining a Simple Function

एक function name() { commands } से defined होता है, जहां curly braces commands के block को wrap करते हैं जो function के body बनाते हैं। function keyword optionally नाम से पहले आ सकता है एक alternative style के रूप में, लेकिन Bash में required नहीं है।

उदाहरण: Defining a Simple Function

bash
#!/bin/bash
greet() {
    echo "Hello from the greet function!"
}
greet

Calling a Function Multiple Times

एक बार define होने पर, एक function script के बाकी हिस्से में जितनी बार ज़रूरत हो call किया जा सकता है, बिल्कुल किसी built-in command की तरह। यह functions का core benefit है: logic एक बार लिखना और इसे जहां भी ज़रूरत हो reuse करना।

उदाहरण: Calling a Function Multiple Times

bash
#!/bin/bash
show_divider() {
    echo "------------------------"
}
show_divider
echo "Section one"
show_divider
echo "Section two"
show_divider

Definitions Must Come Before Use

Bash एक script को top से bottom तक पढ़ता और execute करता है, तो एक function को script के उस line तक पहुंचने तक पहले से define होना चाहिए जो इसे call करती है। Definition से पहले एक function को call करना एक 'command not found' error देता है।

Warning: एक script में सभी functions को top के पास define करना एक common practice है, नीचे calls की main sequence के साथ।

उदाहरण: Definitions Must Come Before Use

bash
#!/bin/bash
say_hi() {
    echo "Hi there!"
}
say_hi

The function Keyword Alternative

Bash function name { commands; } को name() { commands; } के alternative syntax के रूप में भी accept करता है; दोनों एक function को identically define करते हैं।

Parenthesis-based form दूसरे POSIX-compatible shells के लिए ज़्यादा portable है, जबकि function एक Bash/ksh-style extension है।

उदाहरण: The function Keyword Alternative

bash
#!/bin/bash
function farewell {
    echo "Goodbye!"
}
farewell
Related Topics
{# 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. Definition में parameters के साथ parentheses लगाना, जैसे greet(name) { ... }; Bash function definitions parentheses में कभी parameters declare नहीं करते -- वे सिर्फ name() { ... } या function name { ... } हैं, दोनों तरह से empty parens।
  2. यह भूल जाना कि एक function को call होने वाले point से पहले (script में earlier appear) defined होना चाहिए -- Bash top से bottom execute करता है और कुछ अन्य languages की तरह function definitions को hoist नहीं करता।
  3. दूसरी languages की तरह parentheses के साथ एक function call करना, जैसे greet(); Bash में आप इसे arguments के साथ सिर्फ नाम से call करते हैं, space-separated, greet Ada, call site पर किसी parentheses के बिना।
चैप्टर सारांश
  • name() { commands; } या function name { commands; } से define करें -- Bash में दोनों equivalent हैं।
  • एक function को किसी भी दूसरी command की तरह exactly call करें: सिर्फ इसका नाम, optionally space-separated arguments के साथ।
  • Functions को script के execution order में call होने से पहले defined होना चाहिए।
  • एक function का body commands का बस एक group है; इसे किसी special return type declaration की ज़रूरत नहीं है।

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.