Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Syntax of the FUNCTION heading

FUNCTION <functionname>(parameter list) : return value type

In the parameter list, the parameters are separated by a semicolons. It is probably a good idea here to show how some of the standard functions might work. There is of course no way that I can know how they are really coded, but I know what their argument types are and I know what their result types are so I can have a pretty good guess at the coding within them. The first standard function I will reproduce is the standard function SQR(x). This is a fairly simple function, since it takes a real number and returns that number multiplied by itself.

{ -------------------------------------------- } FUNCTION square(a_number : REAL) : REAL; { this function returns the square of its argument } BEGIN square := a_number * a_number; END; { function square } { -------------------------------------------- }

This is called a function declaration. In it you will notice that in the only line of code the result is assigned to the function name. This is how the function returns its result to the program. Every function must do this. This assignment of the result to the function name must be the last action in the body of the function itself. We will look at this later on. The next standard function I will reproduce is the standard function ODD(x). Now the coding of this function is not as difficult as it might first appear. First of all the argument type can only be integer. So how do you know if an integer is odd or even? Answer: divide it by 2 and look at its remainder. In Pascal that is the job of the operator MOD. So if we divide the integer 42 by 2 the answer would be 21 remainder 0. If we divided the integer 37 by 2, the answer would be 18 remainder 1. This means that an odd number when divided by 2 will always have a remainder of one, and an even number will have a remainder of zero. Next, the return value type of the standard function ODD(x) is boolean so if the function argument is odd then the function returns true, and if the function argument is even then the function returns false.

{ ---------------------------------------------------- } FUNCTION is_it_odd(a_number : INTEGER) : BOOLEAN; { this function returns a boolean value TRUE if the argument is an odd number, FALSE if the argument is an even number } BEGIN IF (a_number MOD 2 = 1) THEN is_it_odd := TRUE ELSE is_it_odd := FALSE; END; { function is_it_odd } { ---------------------------------------------------- }

You will notice that in this function, there are two assignment statements assigning a boolean value to the function name. Within the body of the function though there is and IF - THEN - ELSE ~statement, so only one of the assignments will occur when the function is called.

Calling functions

Unlike the standard functions and procedures, programmer-written functions and procedures must be added to the source code of the program. They must also be above any references to them in the source code. We will now look at a little program which calls both of the above functions. There is no problem caused by using the same parameter names in both the functions in the next program. Parameter names are purely local to the procedure declaration, in jargon, this is called scope. The scope of the parameters in the list is limited to their own function or procedure.



Program 1

PROGRAM test_functions(INPUT,OUTPUT); { this program demonstrates how to call functions } VAR real_number : REAL; an_integer : INTEGER; {-----------------------------------------------} FUNCTION square(a_number : REAL) : REAL; { this function produces the square of a number } BEGIN square := a_number * a_number; END; { function square } {-----------------------------------------------} FUNCTION is_it_odd(a_number : INTEGER) : BOOLEAN; { this function returns a boolean value TRUE if the argument is an odd number, FALSE if the argument is an even number } BEGIN IF (a_number MOD 2 = 1) THEN is_it_odd := TRUE ELSE is_it_odd := FALSE; END; { function is_it_odd } {-----------------------------------------------} BEGIN { MAIN PROGRAM } WRITE('Enter a real number : '); READLN(real_number); WRITE('The square of ',real_number:4:2,' is '); WRITELN(square(real_number):4:2); { function called in the WRITELN } WRITE('Enter an integer : '); READLN(an_integer); { function call in IF - THEN condition } IF is_it_odd(an_integer) THEN BEGIN WRITE('The integer ',an_integer:0); WRITELN(' is an odd number!'); END ELSE BEGIN WRITE('The integer ',an_integer); WRITELN(' is an even number'); END; END. { program test_functions }

Now if you run this program you will find that these two functions do work very much like their standard function counterparts. The only problem comes when you type in a negative number to the function is_it_odd. Why is this? Well what is the result of $-15$ divided by 2? Let's write a test program and see! Simple programs, like the next one, are great way to check your understanding of a piece of code from a large program.

Program 2

PROGRAM integer_division(INPUT,OUTPUT); { this is a small test program } BEGIN WRITE('-15 divided by two is: "',-15 DIV 2); WRITELN('" Remainder: "',-15 MOD 2,'"'); END. { program integer_division }

If you run this simple program, you will find that the remainder from dividing a negative number by positive two is -1, so if an_integer is negative the line,

IF (a_number MOD 2 = 1) THEN

will never be true. So how do we change the function is_it_odd to work for negative integers as well as positive integers? Well you might remember that Pascal has the standard function ABS(x) which has the effect of removing a negative sign from its argument. So if we change the above line to,

IF (ABS(a_number MOD 2) = 1) THEN

we will get the results we require. Try this change out. By the way zero is an even number. You may have noticed in chapter three that the list of standard functions did not contain a function TAN although is does contain both the functions COS and SIN. Well the reason for this is that the mathematical function tangent can be defined in terms of the SIN being divided by the COS, this means that you can write a very simple function to produce the tangent of a number or just include a simple piece of code in the main program. Both choices are included in the program tangents below. Before we go on I'd like to show you a more sophisticated version of the is_it_odd function.

{-----------------------------------------------} FUNCTION is_it_odd_2(a_number : INTEGER) : BOOLEAN; { this function returns a boolean value TRUE if the argument is an odd number, FALSE if the argument is an even number } BEGIN is_it_odd_2 := ABS(a_number MOD 2) = 1; END; { function is_it_odd_2 } {-----------------------------------------------}

In this version, the function is done in one line of code. As you can see it uses the explanation above for it's functioning.

Program 3

PROGRAM tangents(INPUT,OUTPUT); { this program calculates the tangent of a given number using a function and without. } VAR number : REAL; {---------------------------------------------} FUNCTION tan(VAR a_value : REAL) : REAL; { function returns the tangent of a number } BEGIN tan := SIN(a_value) / COS(a_value); END; { function tan } {---------------------------------------------} { MAIN PROGRAM } BEGIN WRITE('Enter a number : '); READLN(number); WRITELN; WRITELN('The tangent of ',number:2:4,' is '); WRITELN; WRITE('First answer ',SIN(number)/COS(number):2:4); WRITELN(' radians'); WRITELN; WRITELN('Second answer ',tan(number):2:4,' radians'); END. { program tangents }

When you have run this program you will see that you get the same results in both output lines. That is pretty much the basics of how to write a function in Pascal, but before we move on to writing our own procedures I will show you a slightly more complex function. Suppose we wanted to write a function which could count the number of times that a certain character occurred within a string. It could look something like this.

{ ----------------------------------------------------- } FUNCTION count_characters(VAR in_string : STRING; the_character : CHAR) :INTEGER; { this function returns an integer equal to the number of times the_character appears in the in_string } VAR { declare local variables } I, character_count : INTEGER; BEGIN character_count := 0; FOR I := 1 TO STRLEN(in_string) DO IF (in_string[I] = the_character) THEN character_count := character_count + 1; count_characters := character_count; END; { function count_characters } { ----------------------------------------------------- }

You will notice that this time there really is a parameter list. The first parameter has the reserved word VAR in front of it. This makes in_string what is called in Pascal a variable parameter. We will look more closely at them later. For now though we will look at how this function works. This function has two parameters, the first is a string the second a character to search for. The search algorithm of the function therefore inspects the given string and counts the number of times that character occurs in the given string. It then assigns this count to the function name in the last line. Now in Pascal functions and procedures may have any number of parameters, but functions should only return one data item, and this data item defines the type of the function.

Local Variables

You will notice that the above function count_characters contains the lines,

VAR { declare local variables } I, character_count : INTEGER;

These variables are the function's local variables. This means that these variables cannot be referred to outside of this particular function, their scope is only within the function itself. The same is true for the local variables contained within a procedure. This allows you to use the same identifier names in different functions and procedures without causing problems for the program, though it might make the program difficult to read.

Global Variables

The variable list which is at the head of the program contains what is called global variables, this is because they are available globally to the whole program, and when they are changed, they are changed for the rest of the program.


Date: 2016-03-03; view: 698


<== previous page | next page ==>
Enumerated types, as array subscripts | Using a variable parameter as an in parameter
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.008 sec.)