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

Kursinnehåll

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

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

book
Pass by Value

When we pass function arguments, there are two ways to do this:

  • By value: the function's variable copies the parameter's value without changing the argument.

  • By reference: the function's variable copies the reference of the parameter by changing the argument.

By value:

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

As you can see by the output, x didn't change since we changed the copy of x in the function incValue().

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 7

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

course content

Kursinnehåll

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

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

book
Pass by Value

When we pass function arguments, there are two ways to do this:

  • By value: the function's variable copies the parameter's value without changing the argument.

  • By reference: the function's variable copies the reference of the parameter by changing the argument.

By value:

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

As you can see by the output, x didn't change since we changed the copy of x in the function incValue().

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 7
Vi beklagar att något gick fel. Vad hände?
some-alt