← Back to Kotlin Course | Chapter 4: Functions | Lesson 1 of 7

Function के Basics

एक function steps की एक नामित recipe है जिसे आप जब भी computer से वही काम फिर से करवाना हो reuse कर सकते हैं।
Syntax
kotlin
fun functionName(param: Type): ReturnType {
    return value
}

fun functionName(param: Type) {   // no return value
}

Declaring a Function

एक function fun से शुरू होता है, उसके बाद उसका नाम, parentheses में एक parameter list, एक optional return type, और curly braces में एक body।

उदाहरण: Declaring a Function

markup
// Define the function `greet` taking `name` and returning a String
fun greet(name: String): String {
    // Return `"Hello, $name!"` from this function
    return "Hello, $name!"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `greet("Kotlin")` to the console, with a trailing newline
    println(greet("Kotlin"))
}

Functions with No Return Value

कोई function जो सिर्फ printing जैसा action करता है और कोई उपयोगी value produce नहीं करता उसका return type Unit होता है, जिसे छोड़ा जा सकता है क्योंकि यह default है।

उदाहरण: Functions with No Return Value

markup
// Define the function `printBanner` taking `text`
fun printBanner(text: String) {
    // Print "=== $text ===" to the console, with a trailing newline
    println("=== $text ===")
}

// Entry point: execution of the program starts here
fun main() {
    printBanner("Welcome")
}

Multiple Parameters

कोई function commas से अलग किए कई typed parameters ले सकता है, और इनमें से हर एक (अगर उसका कोई default न हो) को call site पर देना ज़रूरी है।

उदाहरण: Multiple Parameters

markup
// Define the function `add` taking `a`, `b` and returning an Int
fun add(a: Int, b: Int): Int {
    // Return `a + b` from this function
    return a + b
}

// Entry point: execution of the program starts here
fun main() {
    // Print "Sum: ${add(3, 4)}" to the console, with a trailing newline
    println("Sum: ${add(3, 4)}")
}

Calling Functions

किसी function को call करना उसके नाम के बाद parentheses का उपयोग करता है जिनमें parameters declared होने के उसी order में arguments होते हैं।

उदाहरण: Calling Functions

markup
// Define the function `multiply` taking `a`, `b` and returning an Int
fun multiply(a: Int, b: Int): Int {
    // Return `a * b` from this function
    return a * b
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `result`, set to `multiply(6, 7)`
    val result = multiply(6, 7)
    // Print "6 x 7 = $result" to the console, with a trailing newline
    println("6 x 7 = $result")
}
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 implicitly Unit return नहीं करता तब return type declare करना भूल जाना, एक compiler error का कारण बनते हुए।
  2. positionally किसी function को call करते समय parameter order को मिला देना, गलत parameters को values भेजते हुए।
  3. यह न समझना कि बिना explicit return और एक block body वाला function implicitly Unit return करता है, आखिरी expression की value नहीं।
चैप्टर सारांश
  • Functions को fun name(parameters): ReturnType { body } से declare किया जाता है।
  • जिस function की कोई मायने रखने वाली return value नहीं है उसका return type Unit है (या छोड़ा जा सकता है)।
  • Parameters typed हैं और, defaults दिए गए न हों तो, function को call करते समय सभी देने चाहिए।
  • return keyword किसी block-bodied function से एक value वापस भेजता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.