So far we have mostly used procedures and functions defined in units. We have actually defined our own procedures (Initialize and OnEnterFrame) in the graphical application template, but we have not discussed this in detail:
program Workspace;
uses FTGraphics;

procedure Initialize;
begin

end;

procedure OnEnterFrame;
begin

end;

begin
  RunGraphicalApp(@Initialize,@OnEnterFrame);
end.
We defined subroutines previously as portions of code within a larger program that perform a specific task that is relatively independent of other code in the program. Since a subroutine is basically a mini-program, it is fitting that the definition of a subroutine is similar to the definition of a program. Subroutines are defined between the start of the program and the main begin-end program block just like variables and constants. For example, look over the following code which defines a function and procedure:
{$APPTYPE CONSOLE}
program Workspace;

function SomeFunc : Integer;
begin
  Write('Enter an integer: ');
  Readln(Result);
end;

procedure SomeProc(S : String; X,Y : Integer);
begin
  Writeln('S = ',S);
  Writeln('X = ',X);
  Writeln('Y = ',Y);
end;

var
  X : Integer;
begin
  SomeProc('test',5,10);
  X := SomeFunc;
  Writeln('You entered ',X);
  X := SomeFunc;
  Writeln('You entered ',X);
  Readln;
end.
Note the differences in the declarations of "SomeProc" and "SomeFunc". To begin with, SomeProc is a procedure and SomeFunc is a function. As we have mentioned, Object Pascal distinguishes between subroutines that do not return values (procedures) and those that do return values (functions). Since almost everything in Object Pascal has to have a type, the type of value returned by a function must be specified using the function FunctionName : SomeType syntax. The value that is returned is defined by the Result variable. The last value assigned to Result inside the function will be what the function returns. You can see that on Line 21 SomeFunc returns a value and stores this value in X. Procedures do not have this syntax because they do not return values. However, you can also see that SomeProc and SomeFunc differ in other respects (e.g., S : String; X,Y : Integer). These are the formal arguments of a subroutine.  A subroutine may take any number of arguments for processing. When arguments are passed to a subroutine we call them actual arguments. The formal arguments define what kinds of actual arguments can be passed to a subroutine when it is called. Note that when we call SomeProc in the main begin-end block of the program that we pass a string followed by 2 integers. If we had passed an integer instead of a string first, the compiler would have thrown an error (this would have been a semantic compiler error). Although not all languages are this strict in enforcing types of arguments passed to subroutines, strict type checking helps catch errors at compile time so they do not become bugs later on. Procedures and functions can both have formal arguments and they are declared in identical ways (note that in this example we chose not to have any arguments for SomeFunc). Formal arguments are declared exactly like variables. As always, each argument must have a type (n.b., There are cases in which subroutine arguments can be untyped in Object Pascal, but we will deal with untyped arguments later).

Although this is a lot to absorb, declaring procedures and functions is actually quite straightforward and intuitive. The following exercise will help:
Excercise 2-17.
Write code to solve the folowing problems.
  1. Define a procedure that draws a filled of some color C circle given coordinates X and Y and radius R. You will of course need to start with the graphical app template code in order to test your procedure.
  2. Use this procedure to draw 20 concentric circles (circles within circles) at the origin with each step increasing the radius by 1. Set the color to Random(16777215).