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

Function के Basics

एक function instructions का एक नामित box है जिसे आप सिर्फ इसका नाम बोलकर बार-बार चला सकते हैं।
Syntax
rust
fn name() {
    // body
}

name();

Declaring a Function

Functions fn keyword से define होते हैं, उसके बाद एक नाम, एक parenthesized parameter list, और curly braces में एक body। नाम से function call करना इसकी body execute करता है।

उदाहरण: Declaring a Function

markup
// 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();
}

Function Naming Convention

Rust convention functions और variables को snake_case में नाम देना है -- underscores से अलग किए सभी lowercase words। Compiler इससे deviate करने पर एक style warning भी देगा।

उदाहरण: Function Naming Convention

markup
// 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();
}

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

markup
// 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");
}

Calling Functions Multiple Times

एक बार defined होने पर, एक function को ज़रूरत के अनुसार जितनी बार चाहे उतनी बार, जितनी जगहों से चाहे उतनी जगहों से call किया जा सकता है, इसे behavior की एक reusable unit बनाते हुए।

उदाहरण: Calling Functions Multiple Times

markup
// 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();
}
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. यह भूल जाना कि Rust function names snake_case उपयोग करते हैं, JavaScript या Java की तरह camelCase नहीं।
  2. किसी function को उसके define होने से पहले इस तरह call करने की कोशिश करना जो यह मानती है कि source order मायने रखता है -- Rust उसी file में बाद में defined functions call करने देता है।
  3. बिना किसी 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:

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.