म
MarathiCodeby Sprout Tech
Documentation topicsFunctions
MarathiCode v0.2.0

Functions

कार्य <नाव>(मापदंड) { … }

कार्य defines a named function with zero or more parameters. A call can be used as a statement on its own or inside an expression.

functions.mr
कार्य नमस्कार_सांगा(नाव) {
    छापा("नमस्कार, " + नाव)
}

कार्य बेरीज(a, b) {
    परत a + b
}

नमस्कार_सांगा("सई")
छापा(बेरीज(10, 20) * 2)
Output (आउटपुट)
नमस्कार, सई
60

Recursion

factorial.mr
कार्य फॅक्टोरिअल(n) {
    जर n <= 1 तर {
        परत 1
    }
    परत n * फॅक्टोरिअल(n - 1)
}

छापा(फॅक्टोरिअल(5))
Output (आउटपुट)
120

Rules

  • A function must be defined (its कार्य statement must have run) before it is called.
  • The number of arguments must match the number of parameters exactly.
  • Arguments are evaluated left to right before the function body runs.
  • Function names are global, even when the कार्य statement is inside a block. Defining a function again with the same name replaces it.
  • A function body sees its own parameters and variables plus global variables, not the variables of the code that called it. See Scope.
  • Lists passed as arguments are shared, not copied: changes made inside the function are visible outside.
  • Functions are not values: they cannot be stored in variables or passed as arguments.
  • The built-in names लांबी, जोडा, वाचा and संख्या cannot be used for new functions.

Calling with the wrong number of arguments is an error:

arguments.mr
कार्य बेरीज(a, b) {
    परत a + b
}
छापा(बेरीज(1))
Output / Error (आउटपुट / त्रुटी)
धावपळ त्रुटी (Runtime Error)
ओळ 4:
    छापा(बेरीज(1))
कार्य 'बेरीज' साठी 2 आर्ग्युमेंट्स आवश्यक आहेत, परंतु 1 दिले.