Lesson 19 of 23

Call by Reference: SwapCall by Reference: Swap

9 min 35 XP

Function-க்கு variable-ன் address-ஐ அனுப்பினால், function அந்த variable-ஐயே மாற்ற முடியும். scanf(&x) இப்படித்தான் வேலை செய்கிறது. பல மதிப்புகளைத் திருப்பித் தரவும் இதுவே வழி.

Pass a variable's address and the function can change the variable itself. That's exactly how scanf(&x) works, and it is also how a function returns more than one value.

Example: edit me and press Run

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

Array-ஐ function-க்கு அனுப்பும்போது அதன் முதல் உறுப்பின் address தான் போகிறது. ஆகவே function array-ஐ மாற்றினால் main-லும் மாறும்.

Passing an array to a function passes the address of its first element, so changes made in the function are visible in main too.

Challenge

+35 XP

void divide(int a, int b, int *q, int *r) என்ற function எழுதுங்கள்: q-ல் ஈவு, r-ல் மீதி. main()-ல் a, b படித்து print செய்யுங்கள். Input: 17 5 Output: Quotient 3, Remainder 2

Write void divide(int a, int b, int *q, int *r) that stores the quotient in q and the remainder in r. In main(), read a and b and print: Input: 17 5 Output: Quotient 3, Remainder 2

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