Functions Fundamentals

 

Introduction to Functions

Like a procedure, a function is an assignment that must be performed to complete a program. Unlike a procedure, a function must return a value. Based on this, the most fundamental syntax of a function is:

function FunctionName : ReturnType;
begin

end;

The function keyword is required to let the compiler know that you are creating a Pascal function. The function keyword must be followed by a name for the function. The name follows the same conventions applied to other Object Pascal objects and we will apply the suggestions reviewed above for a procedure.

You must let the compiler know what type of data the function would return. This is done by typing a colon followed by a valid data type. An example would be:

Function CalculatePerimeter : Double;
begin

end;

The begin and end keywords are required because they would enclose the assignment performed by the function. Between the begin and end keywords, do whatever the function is supposed to do. After performing the assignment for function, you must specify what value the function is returning. There are two ways you can do this. You can assign the desired result to the name of the function. Here is an example:

Function AddTwoNumbers : Integer;
begin
	AddTwoNumbers := 1250 + 48;
end;

You can also assign the intended result to the Result keyword. Here is an example:

Function AddTwoNumbers : Integer;
begin
	Result := 1250 + 48;
end;

As you are in charge of what a function is supposed to do, you have the responsibility of making sure that a function returns the right kind of value as stated by its data type. A function can return:

  • a character:
function ShowSomeCharacter : char;
begin
	Result := 'Z';
end;
  • an integer:
function Natural : Integer;
begin
	Result := 228;
end;
  • a floating number:
function DecimalNumber : Double;
begin
	Result := 12.55;
end;
  • a Boolean value:
function IsMarried : Boolean;
begin
	Result := true;
end;
  • a string:
function CompleteName : string;
begin
	Result := 'I showed you mine. Now show me yours';
end;

 

 

Function Call

Once a function exists or is known, such as the above simple AddTwoNumbers function, you can call it the same way we did with the above StopHere procedure, by simply typing its name where the function is needed.

We mentioned that the main difference between a function and a procedure is that a function returns a value. This value can be any of the types of data we reviewed previously but the value must be the type stated by the ReturnType word. Because a function returns a value, it can be “passed’ to a Write or a Writeln procedure to display its return value. For example, the above AddTwoNumbers function can be supplied to a Writeln procedure as follows:

program Project1;

{$APPTYPE CONSOLE}

Procedure StopHere;
begin
	write('Press any key to continue...');
	readln;
end;

Function AddTwoNumbers : Integer;
begin
	Result := 1250 + 48;
end;

begin
	Writeln(AddTwoNumbers);
	StopHere
end.

This would produce:

1298
Press any key to continue...

Because a function returns a value, it can also be assigned to a variable of the same type. A function can be used only as the right value in an assignment operation, never to the left of the operator.

Here is an example:

program Project1;

{$APPTYPE CONSOLE}

Procedure StopHere;
begin
	write('Press any key to continue...');
	readln;
end;

Function AddTwoNumbers : Integer;
begin
	Result := 1250 + 48;
end;

var Addition: Integer;
begin
	Addition := AddTwoNumbers;

	StopHere
end.

This means that, once a function has performed its assignment and returns a value, the function call can be used anywhere its type of value is needed.

Procedures and functions are sometimes commonly referred to as routines. Therefore, from now on, in this book, the word “routine” will mean “procedure or function”.



Previous Copyright © 2004 FunctionX Next