Lesson 8 of 24

References और Pass by Reference

9 min 30 XP

Reference (int& r = x) किसी variable का दूसरा नाम है। Parameter को reference बनाएँ तो function बिना pointers के caller का variable बदल सकता है। बड़े objects को copy किए बिना भेजने के लिए const& लें।

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

Pointer बनाम reference: reference null नहीं हो सकता और उसे दूसरे variable की ओर मोड़ा नहीं जा सकता। Interview का लोकप्रिय सवाल।

Challenge

+30 XP

void sortThree(int& a, int& b, int& c) लिखें जो तीनों values को बढ़ते क्रम में लगाए। main() में तीन संख्याएँ पढ़ें, इसे call करें और print करें। Input: 9 2 5 → Output: 2 5 9

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.