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

Function के Parameters

Functions वे values receive करते हैं जिनके साथ उन्हें call किया गया था as positional parameters, $1, $2, और इसी तरह, बिल्कुल वैसे ही जैसे एक पूरी script command-line arguments receive करती है।
Syntax
bash
function_name() {
  echo "$1" "$2"    # arguments
  echo "$#"         # argument count
}

function_name arg1 arg2

Accessing Arguments with $1, $2...

जब एक function arguments के साथ call किया जाता है, वे values उस call की duration के लिए function के अपने $1, $2, और इसी तरह बन जाती हैं, बिल्कुल एक script के command-line arguments की तरह।

यह एक single function को अलग-अलग behave करने देता है depending on क्या दिया गया है।

उदाहरण: Accessing Arguments with $1, $2...

bash
#!/bin/bash
greet() {
    echo "Hello, $1! You are $2 years old."
}
greet "Ada" 36

Counting Arguments with $#

एक function के अंदर $# आपको exactly बताता है कि उस particular call में कितने arguments pass किए गए, जो input को उपयोग करने से पहले validate करने के लिए उपयोगी है।

यह count function तक scoped है -- यह script के अपने top-level argument count को reflect नहीं करता।

उदाहरण: Counting Arguments with $#

bash
#!/bin/bash
sum_two() {
    if [ "$#" -ne 2 ]; then
        echo "Error: expected exactly 2 arguments, got $#"
        return 1
    fi
    echo "Sum: $(( $1 + $2 ))"
}
sum_two 3 4
sum_two 3 || echo "Handled the argument-count error gracefully"

Forwarding All Arguments with "$@"

एक function के अंदर "$@" उस function को pass किए हर argument में expand होता है, हर एक अपना अलग word preserved, जो सभी arguments को किसी और command को forward करने का correct तरीका है।

यह functions के लिए वैसे ही काम करता है जैसे पूरी scripts के लिए।

उदाहरण: Forwarding All Arguments with "$@"

bash
#!/bin/bash
log_message() {
    echo "[LOG] $*"
}
notify_all() {
    log_message "$@"
}
notify_all "System" "started successfully"

Default Values for Missing Parameters

बिल्कुल script arguments की तरह, एक function ${1:-default} style parameter expansion उपयोग कर सकता है जब caller एक argument omit करे तो एक fallback supply करने के लिए। यह एक unset positional parameter reference करने से होने वाली errors से बचाता है।

उदाहरण: Default Values for Missing Parameters

bash
#!/bin/bash
set_volume() {
    local level="${1:-50}"
    echo "Volume set to $level"
}
set_volume 80
set_volume
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. यह मान लेना कि एक function caller के local variables को नाम से automatically access कर सकता है; जब तक explicitly एक argument के रूप में pass न किया जाए (या variable global हो), एक function सिर्फ वह देखता है जो उसे $1, $2, आदि के through दिया जाता है।
  2. एक function के $1 को script के अपने $1 के साथ confuse करना; एक function के अंदर, positional parameters temporarily function के अपने call arguments से replace हो जाते हैं, उस call की duration के लिए script-level वालों को shadow करते हुए।
  3. एक function के arguments को किसी और command को forward करते समय "$@" quote करना भूल जाना, जिससे spaces वाले arguments अलग split हो जाते हैं।
चैप्टर सारांश
  • एक function के अंदर, $1, $2, ... उस function के अपने arguments को refer करते हैं, script के original command-line arguments को नहीं।
  • एक function के अंदर $# उस specific function call को pass किए गए arguments की संख्या देता है।
  • एक function के अंदर "$@" उस function के सभी arguments में expand होता है, हर एक quoted होने पर एक अलग word के रूप में preserved।
  • Functions को पहले से parameter names declare करने की ज़रूरत नहीं है -- आप बस body में जहां भी ज़रूरत हो $1, $2, आदि reference करते हैं।

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.