Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Pass by Reference | Functions
C++ Intermediate | Mobile-Friendly
course content

Kursinnhold

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
Pass by Reference

By reference:

12345678910
void incValue(int &x) { &nbsp;&nbsp;&nbsp;&nbsp;x++; } int main() { &nbsp;&nbsp;&nbsp;&nbsp;int x = 1; &nbsp;&nbsp;&nbsp;&nbsp;incValue(x); &nbsp;&nbsp;&nbsp;&nbsp;cout << x; &nbsp;&nbsp;&nbsp;&nbsp;return 0; }
copy

Here we change x as the function works with the address (reference) of this variable. It can be useful in case you need to change the value of the parameters.

question mark

What the main difference between passing by value and by reference?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 8

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

course content

Kursinnhold

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
Pass by Reference

By reference:

12345678910
void incValue(int &x) { &nbsp;&nbsp;&nbsp;&nbsp;x++; } int main() { &nbsp;&nbsp;&nbsp;&nbsp;int x = 1; &nbsp;&nbsp;&nbsp;&nbsp;incValue(x); &nbsp;&nbsp;&nbsp;&nbsp;cout << x; &nbsp;&nbsp;&nbsp;&nbsp;return 0; }
copy

Here we change x as the function works with the address (reference) of this variable. It can be useful in case you need to change the value of the parameters.

question mark

What the main difference between passing by value and by reference?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 8
Vi beklager at noe gikk galt. Hva skjedde?
some-alt