म
MarathiCodeby Sprout Tech
Documentation topicsReturn Values
MarathiCode v0.2.0

Return Values

परत <मूल्य>

परत ends the function immediately and gives a value back to the caller. It always needs a value after it.

return.mr
कार्य मोठी_संख्या(a, b) {
    जर a > b तर {
        परत a
    }
    परत b
}
छापा(मोठी_संख्या(7, 12))
Output (आउटपुट)
12

Returning from inside a loop

find.mr
कार्य शोधा(यादी, हवे) {
    साठी (i = 0; i < लांबी(यादी); i = i + 1) {
        जर यादी[i] == हवे तर {
            परत i
        }
    }
    परत -1
}
छापा(शोधा(["आंबा", "केळी", "पेरू"], "केळी"))
छापा(शोधा(["आंबा"], "द्राक्ष"))
Output (आउटपुट)
1
-1

Returning several values

Return a list:

min_max.mr
कार्य किमान_कमाल(a, b) {
    जर a < b तर {
        परत [a, b]
    }
    परत [b, a]
}
चल उत्तर = किमान_कमाल(9, 4)
छापा(उत्तर[0])
छापा(उत्तर[1])
Output (आउटपुट)
4
9

Functions without परत

A function that ends without परत produces an empty value, which prints as रिक्त and counts as false in conditions.

no_return.mr
कार्य फक्त_छापा() {
    छापा("आत")
}
छापा(फक्त_छापा())
Output (आउटपुट)
आत
रिक्त

The keyword रिक्त writes the empty value directly, so a result can be compared with it:

none_literal.mr
कार्य शोधा(यादी, x) {
    साठी (i = 0; i < लांबी(यादी); i = i + 1) {
        जर यादी[i] == x तर {
            परत i
        }
    }
    परत रिक्त
}
छापा(शोधा([4, 7, 9], 7))
छापा(शोधा([4, 7, 9], 5) == रिक्त)
Output (आउटपुट)
1
खरे
Using परत outside a function is a runtime error.