Monday, March 21, 2011

Procedures and Functions


"Continuous eloquence wearies. ...Grandeur must be abandoned to be appreciated. Continuity in everything is unpleasant. ...Nature acts by progress, itus et reditus. It goes and returns, then advances further, then twice as much backwards, then more forward than ever, etc. The tide of the sea behaves in the same manner; and so apparently does the sun in its course." Blaise Pascal, Pensées (1669)

Good programming style can be assisted by writing a program in pseudo-code first, to ensure that the logic is sound, is preferable as a first stage before ensuring the syntatic rules are correct. Readability of a program is is enhanced with indentation. Finally, the task of a program is assisted by breaking large problems into smaller subtasks which are discrete and abstract from the particular task.

It is with the latter in mind that functions and procedures are introduced. Pascal includes a number of standard functions, especially for arithmetic. For example the sqrt function, for square roots takes the value of an integer or real and reveals a real value. The value of sqrt(16) is 4.0. A function consists of an argument and a returned value. In this case the argument is 16 and the value returned is 4.0. Not surprisingly arithmetic functions can be combined with larger problems.

The following represents some of the predefined Pascal functions:


FunctionDescriptionArgument TypeReturned Value
absabsolute valueinteger, realinteger
roundroundingrealinteger
trunctruncationrealinteger
sqrsquaringinteger, realinteger, real
sqrtsquare rootinteger, realreal


Further in the interest in breaking down tasks into subtasks, Pascal allows for procedures. The main body of a program makes a procedure call which invokes what is effectively a subprogram. A procedure has any number of formal parameters with their specificied type. When a procedure is called the actual variable paramters are given to the procedure and returned after calculation. The scope of a variable or constant declared in a procedure remains within that procedure and any nested procedures. A variable or constant declared within the main scope of a program is universal for that program.

The following example, based on the 628 AD solution to quatric equations by the Indian mathematician Brahmagupta, makes use of twp arithmetic functions (square and square root), a procedure allowing input of the values, and the passing of values from the main program to the Quadvalues procedure.


program Quad;
var a, b, c, x : real;
procedure Quadvalues(var x, y, z : real); {Note that the variables differ in the procedure to the main program}
begin
writeln('Please enter the values for the a, b and c for a quadratic equation');
readln(x, y, z);
end;
begin {main}
Quadvalues(a,b,c);
x := (sqrt(4*a*c+sqr(b))-b)/(2*a) ;
writeln('The quadratic solution for x with the values', a, b, c);
writeln(x);
end.


Compilation and executing the program results in the following:


lev@squirrel:~/programming/pascal$ fpc quad.pas
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for i386
Compiling quad.pas
Linking quad
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
13 lines compiled, 0.1 sec
lev@squirrel:~/programming/pascal$ ./quad
Please enter the values for the a, b and c for a quadratic equation
12 52 44
The quadratic solution for x with the values 1.20000000000000E+001 5.20000000000000E+001 4.40000000000000E+001
7.24891928816246E-001

No comments: