Computer Science Canada

procedure help

Author:  Schaef [ Tue Jan 20, 2004 7:13 am ]
Post subject:  procedure help

I was just wondering in a procedure what the things in the bracket are after proc and the name of the procedure and why the variables do not need to be assigned values

Author:  CjGaughan [ Tue Jan 20, 2004 10:38 am ]
Post subject: 


A part of a program (a subprogram) can be given a name. There are two kinds of subprograms:

1) Functions
2) Procedures

There are subprograms that are predefined (provided as part of the Turing language).

These include:

-sqrt -- a predefined function that gives the square root of a number.

randint - a predefined procedure that generates random integers

A function takes a value, such as 3, and produces a result, such as 9. In this example, the function produces the square of the number it receives; so when it receives 3 it produces 3**2 = 9


function square (x : real) : real
result x**2
end square

for i : 1 .. 5
put i : 2, square (i) : 4
end for

We say that the square function is called and that actual parameter i is passed to formal parameter x.


The function shown here takes a name such as "fred jones" and produces a result string with the last name first : "Jones, Fred"

basically, the part in the brackets is the kind of variable that will be used for input.

here is an example code (calculates area of a circle):

const pi := 3.141592654
function circleArea (rad : real) : real
    var area : real
    area := pi * rad ** 2
    result area
end circleArea
var r : real
put "Enter the radius of the circle: " ..
get r
put "The area of that circle is: ", circleArea (r) : 5 : 2
var choice : string
put ""

dont know if this helps...but oh well