-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
36 lines (30 loc) · 937 Bytes
/
Copy pathtest.cpp
File metadata and controls
36 lines (30 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
// Übergabe per Wert:
// x ist eine KOPIE von a.
// Die Funktion liest den Wert, ändert nichts am Original.
int funktion1(int x){
return x; // gibt die Kopie von a zurück
}
// Übergabe per Wert:
// x ist eine KOPIE von b.
// Die Änderung betrifft NUR die Kopie.
int funktion2(int x){
x = 5; // ändert nur die lokale Kopie x
return x; // gibt 5 zurück, b bleibt unverändert
}
// Übergabe per Referenz:
// x ist ein ALIAS (Zweitname) für c.
// Änderungen wirken direkt auf c.
int funktion3(int& x){
x = 7; // setzt c auf 7
return x; // gibt den neuen Wert von c zurück
}
int main (){
int a = 1;
int b = 2;
int c = 3;
std::cout << "a: " << funktion1(a) << "\n"; // Ausgabe: 1, a bleibt 1
std::cout << "b: " << funktion2(b) << "\n"; // Ausgabe: 5, b bleibt 2
std::cout << "c: " << funktion3(c) << "\n"; // Ausgabe: 7, c wird 7
return 0;
}