References

From Chilipedia
Revision as of 20:04, 6 July 2018 by Rudra (Talk | contribs) (Created page with "References are a way to access one variable with two names. References are useful when we want to pass a variable to a function and make changes to it inside that function. =...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

References are a way to access one variable with two names. References are useful when we want to pass a variable to a function and make changes to it inside that function.

Using References

To understand why we need references let's look at an example:

If we wanted to move our player to the right, one of the ways we could do it is:

// We have x position for our player on screen;
int posX = 50;
MoveRight(posX); // Should set posX to 51
DisplayValue(posX); // Should display 51 on Screen


// We create a function to move our player 1 unit in x direction
void MoveRight(int x)
{
// Add 1 to the value 
    x = x + 1;
}
// Takes in a variable and Displays its value on the screen
DisplayValue(int x)
{
    // Do some stuff to display the variable on screen
}

After running this code you would expect 51 to be displayed on screen. But when you run this code what would actually be displayed is:

//output
50

This happens because when you pass posX to the MoveRight function, it creates a copy of posX and does all the operations on that copy:

// x position of player on screen
int posX = 50;
MoveRight(posX);  // Here a copy of posX is created and passed into the MoveRight function
DispayValue(posX); // Since a copy of posX was incremented and the actual variable is untouched we get an output of 50; 

void MoveRight(int x)
{
     // Add 1 to the value
    x = x + 1;    // The copy of posX is incremented by 1
}

Passing by Reference

Now, to move our player we want actual value of posX to change and not of a copy, so we need to grab c++ by the collar and tell it "listen to me, I do NOT want you to create a copy of my variable but change the original variable instead". If we change our MoveRight function to:

// Add an & after int - This tells c++ not to copy the value of whatever variable is passed in but to use 'x' as an alias for that variable
void MoveRight(int& x)
{
    // x is an alias for posX
    x = x + 1;
}

Now if we were to run the same code:

 
int posX = 50;
MoveRight(posX);  // Now the address of posX is passed onto x and it becomes an alias for posX 
DispayValue(posX); // Here we get an output of 51

// Add an & after int - This tells c++ not to copy the value of whatever variable is passed in but to use 'x' as an alias for that variable
void MoveRight(int& x)
{
    // x is an alias for posX
    x = x + 1;
}

Here by using the & sign when declaring the function, we instruct c++ not to copy the variable passed but use x as another name for posX. So whatever changes you make to x will be reflected on posX. This is also called passing by reference.

What's really going on here tho

We know that we can create multiple variables, you can also think of variables as boxes where you can store data. But where are those boxes actually stored? In your computers memory. And how does your computer find that box when it needs to access the value ? It does it just like a postman finds your house when you get a letter i.e. by looking up your address.


Similarly, each of these boxes (variables) have addresses just like houses on a street would. So when you add the '&' sign like we did in the example above, you are telling c++ : Every time someone passes a variable into this function, take the address of that variable and refer to data at that address using the newly declared variable (x in our case).


Passing by reference is also faster than passing by value as you are not creating copies of the variable every time you call the function.

Planet Chili Tutorial Video

Other Tutorials

See Also