Parameters और Return Values
In this page:
fn name(param: Type) -> ReturnType {
// body
value
}
Function Parameters
Parameters एक function को input values accept करने देते हैं। हर parameter को एक explicit type दिया जाना चाहिए, क्योंकि Rust कभी function signatures infer नहीं करता।
उदाहरण: Function Parameters
// 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");
}
Login to try C/C++/Java/PHP code in the editor
Returning a Value
किसी function का return type एक arrow -> के बाद declare किया जाता है। function body में final expression, बिना trailing semicolon के लिखा गया, returned value बन जाता है।
उदाहरण: Returning a Value
// 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));
}
Login to try C/C++/Java/PHP code in the editor
Multiple Parameters
Functions commas से अलग, हर एक अपने type के साथ, कई parameters accept कर सकते हैं। इन्हें function body के अंदर बिल्कुल local variables की तरह उपयोग किया जाता है।
उदाहरण: Multiple Parameters
// 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));
}
Login to try C/C++/Java/PHP code in the editor
Early Return with the return Keyword
return keyword एक दी गई value के साथ तुरंत किसी function से बाहर निकलता है, जो function के main logic चलने से पहले special cases जल्दी handle करने के लिए उपयोगी है।
उदाहरण: Early Return with the return Keyword
// 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));
}
Login to try C/C++/Java/PHP code in the editor
- किसी function में final expression के बाद एक semicolon जोड़ना, जो इसे एक returned value से एक discarded statement में बदल देता है।
- जब कोई function एक value return करने के लिए बना हो तो
-> Typeलिखना भूल जाना, return site पर एक compile error का कारण बनते हुए। returnkeyword हर जगह आदतन उपयोग करना जब idiomatic tail-expression style ज़्यादा साफ़ होता।
- Parameters parentheses के अंदर
name: Typeके रूप में declare होते हैं, और हर parameter का एक explicit type होना चाहिए। - किसी function का return type
->के बाद लिखा जाता है, और आखिरी expression (बिना semicolon के) अपने आप return हो जाता है। returnkeyword एक 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: