Functions Define और Call करना
In this page:
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
#!/bin/bash
greet() {
echo "Hello from the greet function!"
}
greet
Login to try C/C++/Java/PHP code in the editor
Calling a Function Multiple Times
एक बार define होने पर, एक function script के बाकी हिस्से में जितनी बार ज़रूरत हो call किया जा सकता है, बिल्कुल किसी built-in command की तरह। यह functions का core benefit है: logic एक बार लिखना और इसे जहां भी ज़रूरत हो reuse करना।
उदाहरण: Calling a Function Multiple Times
#!/bin/bash
show_divider() {
echo "------------------------"
}
show_divider
echo "Section one"
show_divider
echo "Section two"
show_divider
Login to try C/C++/Java/PHP code in the editor
Definitions Must Come Before Use
Bash एक script को top से bottom तक पढ़ता और execute करता है, तो एक function को script के उस line तक पहुंचने तक पहले से define होना चाहिए जो इसे call करती है। Definition से पहले एक function को call करना एक 'command not found' error देता है।
उदाहरण: Definitions Must Come Before Use
#!/bin/bash
say_hi() {
echo "Hi there!"
}
say_hi
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
function farewell {
echo "Goodbye!"
}
farewell
Login to try C/C++/Java/PHP code in the editor
- Definition में parameters के साथ parentheses लगाना, जैसे
greet(name) { ... }; Bash function definitions parentheses में कभी parameters declare नहीं करते -- वे सिर्फname() { ... }याfunction name { ... }हैं, दोनों तरह से empty parens। - यह भूल जाना कि एक function को call होने वाले point से पहले (script में earlier appear) defined होना चाहिए -- Bash top से bottom execute करता है और कुछ अन्य languages की तरह function definitions को hoist नहीं करता।
- दूसरी 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 की ज़रूरत नहीं है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: