Function की Basics
एक function एक recipe card जैसा है जो आप एक बार लिखते हैं और बार-बार उपयोग करते हैं। आप इसे एक नाम देते हैं, और हर बार जब आप वह नाम बोलते हैं computer steps follow करता है।
In this page:
Syntax
function_name() {
commands
}
function_name
Function Basics
एक Bash function function_name() { ... } उपयोग करके commands को एक नाम के तहत group करता है, आपको same logic बिना code duplicate किए बार-बार call करने देते हुए।
Functions को script में call होने से पहले define होना चाहिए। ये किसी भी दूसरे command की तरह ही invoke होते हैं, उनका नाम लिखकर।
Note:
नाम से पहले
function keyword Bash में optional है — greet() { ... } और function greet { ... } दोनों काम करते हैं।उदाहरण: Function Basics
#!/bin/bash
greet() {
echo "Hello from a function!"
}
greet
greet
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
greet() { ... }लिखना और फिर इसेgreet()के रूप में call करना, जब यह सिर्फgreetसे call होता है।- एक function को इसके definition से पहले call करना।
- एक existing command जैसे ही नाम वाला function redefine करना, जो script में उस command को hide कर देता है।
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- Function Basics
- The read Command and Here-Strings
- Function Arguments
- stdin, stdout, stderr and File Descriptors
- Return Values
- Redirection Operators
- Local Variables
- Pipes and Chaining Commands
- Recursive Functions
- Here-Documents (<<EOF)
- Function Libraries
- The tee Command
- Discarding Output with /dev/null