Unit 6.3 - Functions
PROG
read in the dimension
compute the area of the square side = dimension
compute the area of the circle radius=dimension
compute the area of the square side = 2*dimension
compute the area of the circle radius=2*dimension
(same as Unit 6.1)
PED
to learn functions; intro to local variables
CONCEPTS
function proc-name(identifier1:type;identifier2:type;identifier3:type...):ftype;
begin
proc-name:=value to return
end;
local variables
proc or function declaration
var (declarations for local variables--same syntax as for main
program)
begin
end;
local variables -- only known in the main program
In our previous program, we developed a procedure that determined the area of an object. How did we get the value of this area back to the main program where it could be printed out or otherwise used?
We simply stuck the answer in a global variable, Area. However, we may not always want the answer in the same place. It also would be convenient, if we could just use the answer in an expression, a write statement or an if or while condition.
There are many built-in functions. A good example is sqrt. It takes a single argument. It has a value. That value is the square root of the parameter.
If we wanted to perform:
z <- SQRT {X}
(1)We would write:
Z:=SQRT(X);
If we wanted:
Z <- SQRT X + SQRT Y
(2)we would write:
Z:=SQRT(X)+SQRT(Y);
If we wanted:
Z <- SQRT { SQRT X + Y + SQRT Z }
(3)we would write:
Z:=SQRT ( SQRT (X) + Y + SQRT(Z));
Somewhere in the bowels of TurboPascal is something that looks like this:
function SQRT( BLAH : REAL):REAL;
begin
compute the SQRT of BLAH somehow
SQRT:=....
end;
A function has all the properties of a procedure when it comes to parameters. It can have regular parameters and var parameters. They are defined just as they would be for a procedure.
The first line of the procedure does differ from the first line of a procedure--the one containing the word procedure. First of all we write "function" instead of "procedure" to tell Turbo Pascal that we have a function. Also, note that just before the semicolon is a colon and a type such as integer, string or real. In the above example, SQRT is a real function. It returns a value that is a real. Thus we end thefirst line with ":REAL;"
The last issue is how we indicate to Turbo-PASCAL inside the function what value we return.
We simply write at the end of the function---
proc-name:= value to return
proc-name is not really a variable even if it can appear on the left hand side of an assignment statemennt. If you use it on the right hand side, you will try to call the function inside itself --recursion. There are times you want to do this, but not in this course. You will see recursion in CS202 and CS350.
Now let's introduce the concept of local variables
If you write:
function or procedure header
var local_variable1:type1
local_variable2:type2
....
begin
end;
local_variable1 and local_variable2 are known only in the procedure or function. Should you use the same name for a variable in the main program or another function, THEY ARE TWO DIFFERENT BOXES. This is quite useful. This prevents conflicts betweens the variable names chosen for the function and those chosen for the main program. Very often a procedure is written by a different person than the main program. Without the mechanism of local variables, there probably would be conflicts. Often these conflicts involve loop variables such as i,j,m and n.
Now, let's look at sample program.
Recall again that it performs the following four functions.
1. Compute the area of a square whose side is given by "dimension" 2. Compute the area of a circle whose radius is given by "dimension" 3. Compute the area of a square whose side is given by 2*dimension 4. Compute the area of a circle whose radius is given by 2*dimensionWe have two functions. The first will compute the Area of a Square. It is on lines 3 to 8.
Note that it takes a single parameter, side, just like we saw in Unit 6.2.
However, also note the ":real;" on line 3 just at the end. This
says that AreaSquare will return a real number. We could use the
form "AreaSquare(argument)" anywhere a real number can be used. We
have a single local variable "Area" We put a value in it on line
6. Then we use the standard ending for a function
proc-name:=value to return.
I could have written:
AreaSquare:=side*side for lines 6 and 7 if I chose.
Likewise, I have a function called AreaCircle that will compute the area of a circle given its radius. It takes one parameter--the radius. It is defined on lines 9 to 14. Note again that the argument will be put in r. AreaCircle is a function of type real. Note the ":real;" at the end of line 9. Note also that lines 12 and 13 could be rewritten:
AreaCircle:=Pi*r*r;
The first call to compute an area is on line 19. Note that it is embedded in the write statement. We just put AreaSquare(dimension) where I would normally put a variable. Sinc eit is of real type, we can print it out with "10:2" format just as I could have been a variable declared real.
We print out the area of the Circle on line 22--again part of a write.
Line 26 computes the Area of a square whose dimension is twice the area.
Line 29 computes the Area of a circle whose dimension is twice the area.