← Back to Rust Course | Chapter 4: Functions | Lesson 2 of 6

Parameters और Return Values

Functions काम करने के लिए information ले सकते हैं, और खत्म होने पर एक answer वापस दे सकते हैं।
Syntax
rust
fn name(param: Type) -> ReturnType {
    // body
    value
}

Function Parameters

Parameters एक function को input values accept करने देते हैं। हर parameter को एक explicit type दिया जाना चाहिए, क्योंकि Rust कभी function signatures infer नहीं करता।

उदाहरण: Function Parameters

markup
// Define the function `greet_person` taking `name`
fn greet_person(name: &str) {
    // Print "Hello, {}!" to the console, substituting in `name` (with a trailing newline)
    println!("Hello, {}!", name);
}

// Entry point: execution of the program starts here
fn main() {
    greet_person("Ada");
}

Returning a Value

किसी function का return type एक arrow -> के बाद declare किया जाता है। function body में final expression, बिना trailing semicolon के लिखा गया, returned value बन जाता है।

उदाहरण: Returning a Value

markup
// Define the function `square` taking `n` and returning an integer
fn square(n: i32) -> i32 {
    n * n
}

// Entry point: execution of the program starts here
fn main() {
    // Print "Square of 6 is {}" to the console, substituting in `square(6)` (with a trailing newline)
    println!("Square of 6 is {}", square(6));
}

Multiple Parameters

Functions commas से अलग, हर एक अपने type के साथ, कई parameters accept कर सकते हैं। इन्हें function body के अंदर बिल्कुल local variables की तरह उपयोग किया जाता है।

उदाहरण: Multiple Parameters

markup
// Define the function `add` taking `a`, `b` and returning an integer
fn add(a: i32, b: i32) -> i32 {
    a + b
}

// Entry point: execution of the program starts here
fn main() {
    // Print "3 + 4 = {}" to the console, substituting in `add(3, 4)` (with a trailing newline)
    println!("3 + 4 = {}", add(3, 4));
}

Early Return with the return Keyword

return keyword एक दी गई value के साथ तुरंत किसी function से बाहर निकलता है, जो function के main logic चलने से पहले special cases जल्दी handle करने के लिए उपयोगी है।

उदाहरण: Early Return with the return Keyword

markup
// Define the function `absolute_value` taking `n` and returning an integer
fn absolute_value(n: i32) -> i32 {
    // Check whether `n < 0`
    if n < 0 {
        // Return `-n` from this function
        return -n;
    }
    n
}

// Entry point: execution of the program starts here
fn main() {
    // Print "abs(-8) = {}" to the console, substituting in `absolute_value(-8)` (with a trailing newline)
    println!("abs(-8) = {}", absolute_value(-8));
}
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 में final expression के बाद एक semicolon जोड़ना, जो इसे एक returned value से एक discarded statement में बदल देता है।
  2. जब कोई function एक value return करने के लिए बना हो तो -> Type लिखना भूल जाना, return site पर एक compile error का कारण बनते हुए।
  3. return keyword हर जगह आदतन उपयोग करना जब idiomatic tail-expression style ज़्यादा साफ़ होता।
चैप्टर सारांश
  • Parameters parentheses के अंदर name: Type के रूप में declare होते हैं, और हर parameter का एक explicit type होना चाहिए।
  • किसी function का return type -> के बाद लिखा जाता है, और आखिरी expression (बिना semicolon के) अपने आप return हो जाता है।
  • return keyword एक value के साथ जल्दी किसी function से बाहर निकल सकता है, conditionals के अंदर उपयोगी।
  • बिना return type वाला function implicitly unit type () return करता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

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.