Basic Pascal Tutorial/Chapter 1/Standard Functions: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | 1F - Standard Functions (author: Tao Yue, state: unchanged) | |
| 2 | 2 | ||
| 3 | 3 | Pascal has several standard mathematical functions that you can utilize. For example, to find the value of sin of pi radians: | |
| 4 | 4 | ||
| 5 | 5 | ``` pascal | |
| 6 | 6 | value := sin (3.141592653589793238462643383); | |
| 7 | 7 | ``` | |
| 8 | 8 | ||
| 9 | 9 | Note that the sin function operates on angular measure stated in radians, as do all the trigonometric functions. If everything goes well, value should become 0. | |
| 10 | 10 | ||
| 11 | 11 | Functions are called by using the function name followed by the argument(s) in parentheses. Standard Pascal functions include: | |
| 12 | 12 | ||
| 13 | 13 | | Function | Description | Argument type | Return type | | |
| 14 | 14 | |----------|----------------------------|-----------------|------------------| | |
| 15 | 15 | | abs | absolute value | real or integer | same as argument | | |
| 16 | 16 | | arctan | arctan in radians | real or integer | real | | |
| 17 | 17 | | cos | cosine of a radian measure | real or integer | real | | |
| 18 | 18 | | exp | e to the given power | real or integer | real | | |
| 19 | 19 | | ln | natural logarithm | real or integer | real | | |
| 20 | 20 | | round | round to nearest integer | real | integer | | |
| 21 | 21 | | sin | sin of a radian measure | real or integer | real | | |
| 22 | 22 | | sqr | square (power 2) | real or integer | same as argument | | |
| 23 | 23 | | sqrt | square root (power 1/2) | real or integer | real | | |
| 24 | 24 | | trunc | truncate (round down) | real or integer | integer | | |
| 25 | 25 | ||
| 26 | 26 | For ordinal data types (integer or char), where the allowable values have a distinct predecessor and successor, you can use these functions: | |
| 27 | 27 | ||
| 28 | 28 | | Function | Description | Argument type | Return type | | |
| 29 | 29 | |----|----|----|----| | |
| 30 | 30 | | chr | character with given ASCII value | integer | char | | |
| 31 | 31 | | ord | ordinal value | integer or char | integer | | |
| 32 | 32 | | pred | predecessor | integer or char | same as argument type | | |
| 33 | 33 | | succ | successor | integer or char | same as argument type | | |
| 34 | 34 | ||
| 35 | 35 | Real is not an ordinal data type! That's because it has no distinct successor or predecessor. What is the successor of 56.0? Is it 56.1, 56.01, 56.001, 56.0001? | |
| 36 | 36 | ||
| 37 | 37 | However, for an integer 56, there is a distinct predecessor — 55 — and a distinct successor — 57. | |
| 38 | 38 | ||
| 39 | 39 | The same is true of characters: | |
| 40 | 40 | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 41 | + | ``` | |
| 42 | + | 'b' | |
| 43 | + | Successor: 'c' | |
| 44 | + | Predecessor: 'a' | |
| 45 | + | ``` | |
| 44 | 46 | ||
| 45 | 47 | The above is not an exhaustive list, as modern Pascal compilers include thousands of functions for all sorts of purposes. Check your compiler documentation for more. | |
| 46 | 48 | ||
| 47 | 49 | --- | |
| 48 | 50 | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 1/Standard Functions", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. 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).* |
