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:
12345678910void incValue(int x) { x++; } int main() { int x = 1; incValue(x); cout << x; return 0; }
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?
Tack för dina kommentarer!
Avsnitt 4. Kapitel 7
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Suggested prompts:
Ställ mig frågor om detta ämne
Sammanfatta detta kapitel
Visa verkliga exempel
Awesome!
Completion rate improved to 2.94
Pass by Value
Svep för att visa menyn
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:
12345678910void incValue(int x) { x++; } int main() { int x = 1; incValue(x); cout << x; return 0; }
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?
Tack för dina kommentarer!
Avsnitt 4. Kapitel 7