Up to this point we have dealt with subroutines that execute some series of statements. Subroutines of this nature are called procedures in object pascal. On the other hand, functions are subroutines that execute some series of statements and then return a value.  There are some obvious procedures and functions that would be useful for general-purpose programming. For example, one could define a function that returns the length of a string variable. Or one could write another that returns a number raised to a certain power. All languages define a standard library of subroutines. When learning a new language, one of the first steps is to determine what is available in the standard library because it can often reduce the amount of code you have to write yourself. In Object Pascal, the standard library is composed of a series of units. We have not needed to use any units of the standard library up to this point because the constants, types, procedures, and functions such as True, False, Writeln, Readln, Integer, and Real that we have discussed are predefined by the compiler. The following table lists the most common predefined subroutines in Object Pascal, some of which we have already covered. Look over these subroutines but do not worry about memorizing them just yet. You will be asked to use them in the exercise that follows this table and will be able to refer back to the table in order to pick the correct subroutine. After programming for a while you will intuitively know the names of these procedures and functions and how to use them.
Identifier
Description
Write or Writeln
Prints values to the screen with or without a line break
var
  X : Integer;
begin
  X := 2;
  Writeln('Answer to life, ','the universe, and everything = ',4,X);
  //Output: Answer to life, the universe, and everything = 42
  Readln;
end.
Readln
Reads in user-input values
var
  X : Integer;
begin
  Write('Input an integer: ');
  Readln(X); //Waits for user input
end.
Length
Given a variable, returns the current size of a dynamic variable (like a string)
var
  X : Integer;
begin
  Writeln(Length('test'));
  // Output: 4
  Readln;
end.
SizeOf
Given a variable, returns the actual size in memory of a variable
var
  C : Char;
  I : Integer;
begin
  Writeln(SizeOf(C));
  // Output: 1
  // A char variable takes up 1 byte in memory.
  Writeln(SizeOf(I));
  // Output: 4
  // An integer variable takes up 4 bytes in memory.
  Writeln(SizeOf(Char));
  // Output: 1
  Writeln(SizeOf(Integer));
  // Output: 4

  Readln;
end.
Chr
Given an ASCII code (value between 0 and 255) returns the character represented by that code
begin
  Writeln(Chr(65));
  // Output: A
  Writeln(Chr(66));
  // Output: B
  Writeln(Chr(67));
  // Output: C
  Writeln(Chr(48));
  // Output: 0
  Writeln(Chr(49));
  // Output: 1
  Writeln(Chr(50));
  // Output: 2

  Readln;
end.
Ord
Returns the ordinal value of an "ordered" type (such as an ASCII character)
begin
  Writeln(Ord('A'));
  // Output: 65
  Writeln(Ord('B'));
  // Output: 66
  Writeln(Ord('C'));
  // Output: 67
  Writeln(Ord('0'));
  // Output: 48
  Writeln(Ord('1'));
  // Output: 49
  Writeln(Ord('2'));
  // Output: 50

  Readln;
end.
Random or Random(X)
Random returns a random real number between 0 and 1. Random(X) returns a random integer from 0 to X. Both random numbers are drawn from a uniform distribution.
begin
  Randomize;
  Writeln(Random);
  // Output: Who knows... it's random (though it will be between 0 and 1)

  Writeln(Random(10));
  // Output will be an integer between 0 and 10

  Writeln(Random(100));
  // Output will be an integer between 0 and 100

  Writeln(20+Random(10));
  // Output will be an integer between 20 and 30

  Readln;
end.
There are many more predefined identifiers than the ones listed in these tables, but for now these will suffice. To better understand how these subroutines work, look over the exercise below.
Excercise 2-11.
Write code to solve the following problems.
  1. Read in a string from the user and store it in a String variable. Print the length of this string variable on the screen.
  2. ASCII stands for the American Standard Code for Information Interchange. It was invented so that the numbers from 0 to 255 could represent symbolic characters on a computer. Write a program that will return the ASCII code for a character by reading in text from the user and storing it in a character variable. Hint: You'll need one of the predefined functions too.
  3. Using code similar to the program you created previously, determine the ASCII codes for 0, 9, A, Z, a, z, and Space.
Most other identifiers we will encounter are not predefined by the compiler and are instead defined in the standard library. SysUtils is the most fundamental unit of the Object Pascal standard library. It stands for System Utilities and defines a number of useful types and subroutines. To use SysUtils we simply add a uses clause to the program:
program Workspace;
uses SysUtils;

begin
end.
SysUtils contains string handling routines, memory management routines, number to string conversion routines, date/time routines, and many more. Some of the more useful subroutines from SysUtils are listed in the table below. Again, we will practice with these in the exercise that follows the table.
Subroutine
Description
Format
Formats any number of values into a string according to format specifiers given in FormatStr.
{$APPTYPE CONSOLE}
program Workspace;
uses SysUtils;
var
  X : Real;
begin
  X := 3.14159;
  Writeln(X);
  // Output: 3.14159000000000E+000
  Writeln(Format('%.5f',[X]));
  // Output: 3.14159
  // The percent sign % is called a format specifier. %.5f means a fixed number
  //   of 5 decimal places.
  Writeln(Format('PI = %.5g',[X]));
  // Output: PI = 3.14159
  // Format strings can be composed of any combination of format specifiers and
  //   regular characters.
  Writeln(Format('%d%%',[50]));
  // Output: 50%
  // %d is the integer format specifier. Double percent %% escapes the format
  //   specifier and returns a single percent sign.
  Write(Format('%5d',[52])); Writeln(Format('%5d',[4]));
  Write(Format('%5g',[11.7])); Writeln(Format('%5d',[12]));
  // Output:    52    5
  // Output:  11.7   12
  // Any number without a decimal point between the % and some letter is
  //   considered a width specifier and ensures that numbers with fewer
  //   digits than the specified width are padded with spaces
  Writeln(Format('%.5d',[50]));
  // Output: 00050
  // Since integers cannot have decimals, the .5 in this expression is a padding
  //   specifier. It ensures that there are 5 digits in the output by padding
  //   the output with 0's.
  Writeln(Format('%.5g',[X]));
  // Output: 3.1415
  // %.5g means general formatting with 5 significant figures. Note the
  //   difference in %.5g and %.5f. 3.14159 has 6 significant figures and 5
  //   digits after the decimal point. %.5g drops the last digit because only 5
  //   significant figures were requested.
  Writeln(Format('%.5g %.3d %s',[X,5,'somestr']));
  // Output: 3.1415 005 somestr
  // Any number of values can be combined using format strings but the format
  //   specifier must be appropriate for the type of the value used. For
  //   example, the string formatter %s is not appropriate for the variable X
  //   since it is a number.
  Readln;
end.
CompareStr, Copy, Delete, Insert, Lowercase, Uppercase, Pos, StringOfChar
String handling routines (see example for more info).
{$APPTYPE CONSOLE}
program Workspace;
uses SysUtils;
var
  Str : String;
begin
  // Note that these string-handling routines are almost universal in all
  //   programming languages, but they often go by slightly different names
  //   depending on the language.

  // Usage: CompareStr(Str1,Str2)
  Writeln(CompareStr('test','test2'));
  // Output: 0
  // CompareStr returns -1 if the first string is less than the second (in terms
  //   of alphabetical order), 0 if they are equal, or 1 if the first is greater
  //   than the second (Note that you can also use >, =, and < operators
  //   on strings

  // Usage: Copy(Str,Index,Count)
  Writeln(Copy('test',2,2));
  // Output: es
  // Copy copies characters from a string starting with Index and continuing for
  //   Count characters. Strings in Object Pascal are 1-indexed which means 1 is
  //   the first character, 2 is the second, and so on

  // Usage: Delete(Str,Index,Count)
  Str := 'test';
  Delete(Str,2,2);
  Writeln(Str);
  // Output: tt
  // Delete removes characters from a string (it performs the operation on the
  //   actual variable and does not create a new string like copy)

  // Usage: Insert(NewStr,Str,Index)
  Insert('es',Str,2);
  Writeln(Str);
  // Output: test
  // Insert places the string NewStr into Str at a certain position.

  // Usage: Lowercase(Str)
  Writeln(Lowercase('Test'));
  // Output: test

  // Usage: Uppercase(Str)
  Writeln(Uppercase('Test'));
  // Output: TEST

  // Usage: Pos(SubStr,Str)
  Writeln(Pos('st','test'));
  // Output: 3
  Writeln(Pos('sa','test'))
  // Output: 0
  // Returns the index of the first substring in a larger string. If the
  //   substring cannot be found it returns 0.

  // Usage: StringOfChar(SomeChar,Count)
  Writeln(StringOfChar('A',5));
  // Output: AAAAA
  // Creates a string of repeating characters.

  Readln;
end.
IntToStr, Val
String-number conversion routines
{$APPTYPE CONSOLE}
program Workspace;
uses SysUtils;
var
  X,Code : Integer;
  Num : Real;
  Str : String;
begin
  X := 5;
  Str := IntToStr(X)+' = Five';
  Writeln(Str);
  // Output: 5 = Five

  Str := '42';
  Val(Str,X,Code);
  Writeln(Str+'1');
  // Output: 421
  Writeln(X+1);
  // Output: 43
  // Val converts string into an integer and stores it in X.
  // The expression Str+'1' concatenates a string while the expression X+1 is
  //   arithmetic since X is an integer.

  Str := '4.2';
  Val(Str,Num,Code);
  Readln;
end.
Date, Time, Now, DateTimeToStr, DateToStr, TimeToStr, DayOfWeek, DecodeDate, DecodeTime, EncodeDate, EncodeTime, FormatDateTime
Date/time routines
Again, keep in mind that this is not an exhaustive list of all the subroutines in SysUtils (you can see all of them by right-clicking on SysUtils in a uses clause and selecting Find Declaration). Another very useful unit in the standard library is the Math unit. The Math unit consists of several common mathematical functions.
Subroutine
Usage
Description
Abs
Abs(X)
Returns the absolute value of a number: .
Ceil
Ceil(X)
Rounds X up toward positive infinity: .
Floor
Floor(X)
Rounds X down toward negative infinity: .
Round
Round(X)
Rounds X to the nearest whole number.
Trunc
Trunc(X)
Truncates the decimal part of a real number.
Int
Int(X)
Returns the integer part of a real number (identical to Trunc).
Frac
Frac(X)
Returns the decimal part of a real number.
Sqr
Sqr(X)
Returns the square of X:
Sqrt
Sqrt(X)
Returns the square root of X:
Exp
Exp(X)
Returns the exponential of X:
Power
Power(X,N)
Raises X to the power N:
Ln
Ln(X)
Returns the natural log of X:
Log10
Log10(X)
Returns the base-10 log of X:
Log2
Log2(X)
Returns the base-2 log of X:
LogN
LogN(X,N)
Returns the base-N log of X:
Sin
Sin(Theta)
Returns the sine of angle Theta:
Cos
Cos(Theta)
Returns the cosine of angle Theta:
Tan
Tan(Theta)
Returns the tangent of angle Theta:
SinCos
Tan(Theta)
Returns sine and cosine of an angle (using this function is twice as fast as calling Sin or Cos separately for the same angle).
ArcSin
ArcSin(X)
Returns the inverse sine of a given number:
ArcCos
ArcCos(X)
Returns the inverse cosine of a given number:
ArcTan
ArcTan(X)
Returns the inverse tangent of a given number:
ArcTan2
ArcTan2(Y,X)
Returns the inverse tangent from two numbers with respect to the proper quadrant of an angle:
Min
Min(X1,X2)
Returns the lesser of two numeric values.
Max
Max(X1,X2)
Returns the greater of two numeric values.
RandG
RandG(Mean,Std)
Returns a random number from a Gaussian distribution given that distributions mean and standard deviation:
Excercise 2-12.
Write code to solve the following problems.
  1. Ask the user for a dividend and divisor and then compute the quotient. Return the quotient as a string in general format. Also output the quotient using Writeln(Dividend/Divisor) to compare a formatted number to a non-formatted one.
  2. Ask the user to input a real number and then calculate the base 10 log of that number. Return a string that gives both the number (in general format) and the base 10 log (also in general format but truncated at 5 decimal points).
SysUtils and Math are the primary units we will use from the standard library. There are others, but we do not discuss them here because they introduce programming constructs we have not yet discussed.