Const/de: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 6 | - | ||
| 1 | + | Zurück zu den [[Data type/de|Datentypen]]. | |
| 2 | + | Zurück zu den [[Reserved words/de|reservierten Wörtern]]. | |
| 3 | + | ||
| 7 | 4 | ||
| 8 | - | ||
| 5 | + | ## Contents | |
| 9 | 6 | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 7 | + | * 1 const | |
| 8 | + | * 1.1 Allgemeines zu Konstanten | |
| 9 | + | * 1.2 Unypisierte Konstanten | |
| 10 | + | * 1.3 Typisierte Konstanten | |
| 11 | + | * 1.4 Verwendung **function** /**procedure** -Kopf | |
| 19 | 12 | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 13 | + | ||
| 27 | 14 | ||
| 28 | - | ||
| 15 | + | # const | |
| 29 | 16 | ||
| 30 | - | ||
| 17 | + | ## Allgemeines zu Konstanten | |
| 31 | 18 | ||
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 19 | + | Mit dem Schlüsselwort **const** beginnt die Deklaration der Konstanten. | |
| 20 | + | Es gibt zwei Arten von Konstanten: | |
| 37 | 21 | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 22 | + | * untypisierte Konstanten | |
| 41 | 23 | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 24 | + | * typisierte Konstanten | |
| 25 | + | ||
| 26 | + | ||
| 27 | + | ||
| 28 | + | ## Unypisierte Konstanten | |
| 29 | + | ||
| 30 | + | Einer untypisierten Konstanten kann nur einmal und zwar zum Zeitpunkt ihrer Deklaration (Bekanntmachung, Erstellung) ein Wert zugewiesen werden. Dieser Wert kann während der Laufzeit des Programmes nicht mehr geändert werden. | |
| 31 | + | Für untypisierte Konstanten können einfache Datentypen verwendet werden. | |
| 32 | + | ||
| 33 | + | Beispieldeklaration untypisierter Konstanten: | |
| 65 | 34 | ``` | |
| 35 | + | const | |
| 36 | + | IntegerZahl = 210; // Konstante vom Datentyp Integer | |
| 37 | + | FliesskommaZahl = 2.1; // Konstante vom Datentyp Fliesskomma | |
| 38 | + | TextString = 'Änderung'; // Konstante vom Datentyp String | |
| 39 | + | Buchstabe = 'F'; // Konstante vom Datentyp String und vom Datentyp Char | |
| 40 | + | ``` | |
| 66 | 41 | ||
| 67 | - | ||
| 42 | + | ||
| 68 | 43 | ||
| 69 | - | ||
| 44 | + | ## Typisierte Konstanten | |
| 70 | 45 | ||
| 71 | - | ||
| 46 | + | Bei typisierten Konstanten wird der Konstanten ein bestimmter Datentyp zugewiesen. Damit ist es möglich neben einfachen Datentypen, einer typisierten Konstanten z.B. auch Records und Arrays zuzuweisen. | |
| 47 | + | Im Gegensatz zu untypisierten Konstanten kann der Wert einer typisierten Konstanten zur Laufzeit des Programms geändert werden. | |
| 48 | + | Damit bietet unter Pascal die typisierte Konstante eine ähnliche Funktionalität, wie es in anderen Programmiersprachen der Befehl **static** bietet. | |
| 49 | + | ||
| 50 | + | Beispieldeklaration typisierter Konstanten: | |
| 51 | + | ``` | |
| 52 | + | const | |
| 53 | + | Steuersatz: Single = 18.0; | |
| 54 | + | ||
| 55 | + | // Konstante Array aus 6 Buchstaben. | |
| 56 | + | Alpha: array[1..6] of Char = ('A', 'B', 'C', 'D', 'E', 'F'); | |
| 57 | + | ||
| 58 | + | // Andere Variante für Char. | |
| 59 | + | Ziffer: array[0..5] of char = '123456'; | |
| 60 | + | ||
| 61 | + | // Als record. | |
| 62 | + | rec: record | |
| 63 | + | x, y, z: integer; | |
| 64 | + | end | |
| 65 | + | = (x: 123; y: 456; z: 789); | |
| 66 | + | ||
| 67 | + | // Verschachtelt | |
| 68 | + | rec2: record | |
| 69 | + | x, y, z: integer; | |
| 70 | + | f: record | |
| 71 | + | a, b: single; | |
| 72 | + | end; | |
| 73 | + | c: array[0..5] of char; | |
| 74 | + | end | |
| 75 | + | = (x: 123; y: 456; z: 789; | |
| 76 | + | f: (a: 1.2; b: 3.4); | |
| 77 | + | c: '123456'); | |
| 78 | + | ``` | |
| 72 | 79 | ||
| 73 | - | ||
| 80 | + | ## Verwendung **function** /**procedure** -Kopf | |
| 74 | 81 | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 82 | + | Wird **const** im **function** /**procedure** -Kopf verwendet, dann wird (regelmäßig, nicht zwingend) nur ein Zeiger der Variable übergeben. Bei dem Beispiel mit dem String, würde es ohne **const** den ganzen String bei der Übergabe kopieren. | |
| 80 | 83 | ||
| 81 | - | ||
| 82 | - | ||
| 84 | + | Dies könnte man auch mit [[Var/de|**var**]] machen, aber dann wäre es nicht möglich eine Konstante der function/procedure zu übergeben. | |
| 83 | 85 | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 86 | + | Dafür kann die Variable in der function/procedure **nicht** geändert werden. | |
| 89 | 87 | ``` | |
| 88 | + | procedure Ausgabe(const s :String); | |
| 89 | + | begin | |
| 90 | + | WriteLn(s); | |
| 91 | + | end; | |
| 92 | + | ||
| 93 | + | var | |
| 94 | + | s1: string; | |
| 95 | + | ||
| 96 | + | begin | |
| 97 | + | s1 := 'Ein String'; | |
| 98 | + | Ausgabe(s1); | |
| 99 | + | Ausgabe('Hello World !'); // Auch als Konstante möglich. | |
| 100 | + | end. | |
| 101 | + | ``` | |
| 90 | 102 | ||
| 91 | 103 | --- | |
| 92 | - | ||
| 104 | + | *From the Free Pascal wiki, page "Const/de", as the Internet Archive kept it on 2023-10-07. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
