Programs Using Reference Variables

Learn about reference variables in C++ and their applications.

Let‘s look at an example of how we can use references to solve the problem of getting multiple values from a function.

Division of two numbers

We have a function named divide (as shown in the animation below) that takes four arguments: the “numerator,” “denominator,” “quotient,” and “remainder.”

We are obviously interested in finding the quotient and the remainder when we divide two numbers. Here, instead of returning the quotient and the remainder, we’ll be updating q and r from within the divide() function using reference variables.

Let’s look at the animation.

Get hands-on with 1200+ tech skills courses.