0
0
C++programming~5 mins

Passing parameters by reference in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean to pass a parameter by reference in C++?
Passing a parameter by reference means giving the function access to the original variable, not a copy. Changes inside the function affect the original variable.
Click to reveal answer
beginner
How do you declare a function parameter to be passed by reference in C++?
You add an ampersand (&) after the type in the function parameter list. For example: void func(int &x) means x is passed by reference.
Click to reveal answer
beginner
What is the difference between passing by value and passing by reference?
Passing by value sends a copy of the variable, so changes inside the function do not affect the original. Passing by reference sends the actual variable, so changes affect the original.
Click to reveal answer
intermediate
Can you pass a constant reference? Why would you do that?
Yes, by using <code>const Type &</code>. It allows the function to access the original variable without copying it, but prevents the function from changing it. This is efficient and safe.
Click to reveal answer
intermediate
What happens if you try to pass a literal (like 5) to a function expecting a non-const reference parameter?
It causes a compilation error because literals cannot be bound to non-const references. Only variables can be passed by non-const reference.
Click to reveal answer
How do you declare a function parameter to be passed by reference in C++?
AUse the keyword 'ref' before the type
BAdd an ampersand (&) after the type
CAdd an asterisk (*) after the type
DUse the keyword 'pointer' before the type
What is the main advantage of passing parameters by reference?
AIt makes a copy of the variable for safety
BIt prevents the function from accessing the variable
CIt allows the function to modify the original variable
DIt slows down the program
Which of these will cause a compilation error?
APassing a literal to a function expecting a non-const reference parameter
BPassing a variable to a function expecting a reference parameter
CPassing a variable to a function expecting a const reference parameter
DPassing a pointer to a function expecting a reference parameter
What does the following function signature mean? void update(int &num)
Anum is passed by value
Bnum is a constant
Cnum is a pointer
Dnum is passed by reference
Why would you use a const reference parameter?
ATo prevent copying large objects and avoid modification
BTo allow the function to modify the argument
CTo make a copy of the argument
DTo pass a pointer instead
Explain how passing parameters by reference works in C++ and why it might be useful.
Think about how the function can change the original variable.
You got /3 concepts.
    Describe the difference between passing a parameter by value, by reference, and by const reference.
    Consider what the function can do with the parameter and how it affects the original.
    You got /3 concepts.