Fundamentally, defining a function in Pascal includes the function header, local declarations, and the function body. The following guide on writing functions in Pascal will clarify the basic concepts and how to use functions in Pascal effectively.
Writing Functions in Pascal
Subprograms in Programming
Subprograms, also known as subroutines, are program units/modules that perform specific tasks. These subprograms are combined together to form a larger program. Essentially, this approach is referred to as Modular design.
A subprogram can be called by another program/subprogram, known as the calling program.
Pascal provides 2 types of subprograms:
- Function: this subprogram returns a single value.
- Procedure: this subprogram does not directly return a value.
Function
A function is a group of statements that perform a specific task. Every Pascal program will have at least one function, which is the main program, and all other non-essential programs may define additional functions.
Function declaration communicates to the compiler the name of the function, return value type, and parameters. A function definition furnishes the body of the function.
The standard Pascal library provides numerous prebuilt functions that programs can invoke. For instance, the AppendStr( ) function concatenates two strings, the New( ) function automatically allocates memory for variables, and many other functions.
Function Definition
In Pascal, a function is defined using the function keyword. The general formula for a function definition is as follows:
function name(arguments): type1; arguments): type2; ...): function_type;
declare local variables;
begin
...
< statements=''>
...
name := expression;
end;
Writing Functions in Pascal
A function definition in Pascal comprises the function header (header), local declarations, and the function body. The function header contains the function keyword and the function name. Here are the details of each part of a function:
- Argument (Argument): Arguments establish the link between the calling program and the defined functions, also known as formal parameters (formal parameter). Parameters act as placeholders. When a function is called, you receive values for the parameters.
These values are called actual parameters or arguments. The parameter list refers to the parameter type, order, and number of parameters of a function. Using formal parameters is optional. These parameters can be standard data types, user-defined data types, or subrange data types.
declare
(* declaration of local variables *)
result: integer;
start
if (num1 is greater than num2) then
assign result as num1
otherwise
assign result as num2;
result is set to max;
end of function;
How to write a function in Pascal
Function Declaration
Declaring a function informs the compiler about the function's name and how to invoke it. The body of the function can be defined separately.
A function declaration comprises the following parts:
function name(argument(s): type1; argument(s): type2; ...): function_type;
For the max() function defined above, here's how you declare the function:
function max(num1, num2: integer): integer;
When declaring a function, it's necessary when you define a function in one source file and call that function in another file. In this case, you should declare the function at the beginning of the file where the function is called.
Calling a function
While creating a function, you provide a definition of what the function is supposed to do. To use the function, you'll have to call it to perform a certain task.
When a program calls a function, control is transferred to the called function. The called function performs the defined task, and when the return statement is executed or when the end statement is reached, it returns control to the main program.
To call a function, simply provide the required parameters, and if the function returns a value, you can store this returned value.
Here is a simple example of calling a function:
program exFunction;
var
a, b, ret : integer;
(*function definition *)
function max(num1, num2: integer): integer;
var
(* declaration of local variables *)
result: integer;
begin
if (num1 is greater than num2) then
result := num1
else
result := num2;
max := result;
end;
begin
a := 100;
b := 200;
(* using a function to determine the maximum value *)
ret := max(a, b);
writeln( 'Max value is : ', ret );
end.
Writing Functions in Pascal
When compiled and executed, the code snippet above will result in:
Max value is : 200
Here's how to write functions in Pascal. We hope after reading this article, readers will have a better understanding of Pascal, common Pascal functions, and how to write functions in Pascal. Wishing you all success.