Function के Basics
In this page:
fn name() {
// body
}
name();
Declaring a Function
Functions fn keyword से define होते हैं, उसके बाद एक नाम, एक parenthesized parameter list, और curly braces में एक body। नाम से function call करना इसकी body execute करता है।
उदाहरण: Declaring a Function
// Define the function `greet` with no parameters
fn greet() {
// Print "Hello from a function!" to the console (with a trailing newline)
println!("Hello from a function!");
}
// Entry point: execution of the program starts here
fn main() {
greet();
}
Login to try C/C++/Java/PHP code in the editor
Function Naming Convention
Rust convention functions और variables को snake_case में नाम देना है -- underscores से अलग किए सभी lowercase words। Compiler इससे deviate करने पर एक style warning भी देगा।
उदाहरण: Function Naming Convention
// Define the function `print_welcome_message` with no parameters
fn print_welcome_message() {
// Print "Welcome to Rust!" to the console (with a trailing newline)
println!("Welcome to Rust!");
}
// Entry point: execution of the program starts here
fn main() {
print_welcome_message();
}
Login to try C/C++/Java/PHP code in the editor
Functions Can Be Defined in Any Order
कुछ scripting languages के विपरीत, Rust को source file में उपयोग होने से पहले एक function define होने की ज़रूरत नहीं है -- compiler calls जांचने से पहले file में सभी function definitions resolve करता है।
उदाहरण: Functions Can Be Defined in Any Order
// Entry point: execution of the program starts here
fn main() {
say_hello();
}
// Define the function `say_hello` with no parameters
fn say_hello() {
// Print "Defined after main, but still callable" to the console (with a trailing newline)
println!("Defined after main, but still callable");
}
Login to try C/C++/Java/PHP code in the editor
Calling Functions Multiple Times
एक बार defined होने पर, एक function को ज़रूरत के अनुसार जितनी बार चाहे उतनी बार, जितनी जगहों से चाहे उतनी जगहों से call किया जा सकता है, इसे behavior की एक reusable unit बनाते हुए।
उदाहरण: Calling Functions Multiple Times
// Define the function `cheer` with no parameters
fn cheer() {
// Print "Hooray!" to the console (with a trailing newline)
println!("Hooray!");
}
// Entry point: execution of the program starts here
fn main() {
cheer();
cheer();
cheer();
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि Rust function names
snake_caseउपयोग करते हैं, JavaScript या Java की तरहcamelCaseनहीं। - किसी function को उसके define होने से पहले इस तरह call करने की कोशिश करना जो यह मानती है कि source order मायने रखता है -- Rust उसी file में बाद में defined functions call करने देता है।
- बिना किसी arguments वाला function call करते समय parentheses छोड़ना, जैसे
greet();के बजायgreet;लिखना।
- Functions
fn name(parameters) { body }से declare होते हैं। - Rust function और variable नाम convention से
snake_caseउपयोग करते हैं। - Functions किसी file के अंदर किसी भी order में defined हो सकते हैं; Rust को forward declarations की ज़रूरत नहीं।
mainवह special function है जहां हर executable program शुरू होता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: