Lesson 19 of 23

Call by Reference: Swap

9 min 35 XP

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.

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

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.