Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedures Brent M. Dingle Texas A&M University

Similar presentations


Presentation on theme: "Procedures Brent M. Dingle Texas A&M University"— Presentation transcript:

1 Procedures Brent M. Dingle Texas A&M University
Chapter 4 – Sections 2 and 3 (and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan)

2 Usage of Procedures Procedures are one way to implement top-down design techniques within Pascal. They are used to break long programs into smaller programs. They are also used to avoid repeatedly typing the same (or similar) code over and over within the same program – this often involves the usage of parameters.

3 Definition The simplest way to define a procedure is to say it is a subprogram. The structure layout of a procedure is very similar to that of a simple program as illustrated in the next two slides.

4 Recall Program Layout PROGRAM [name]; { heading } CONST
{ declaration of constants } VAR {declaration of variables } BEGIN { body } END.

5 Procedure Layout PROCEDURE [name](parameters); { heading } CONST
{ declaration of constants } VAR {declaration of variables } BEGIN { body } END;

6 Layout similarity So both programs and procedures have headings, declarations and bodies. Thus thinking of a procedure as a mini-program inside a program is usually a good analogy.

7 Add a procedure Say we had the program on the next slide and we wanted to alter it so it used a procedure. Why? Because it makes an easy example. =)

8 Simple program PROGRAM ProcEx; VAR first_name, last_name : string[50];
BEGIN { main body } write(‘Please enter your first name -> ‘); readln(first_name); write(‘Please enter your last name -> ‘); readln(last_name); writeln(first_name, last_name, ‘… such a nice name.’); writeln(‘You must be a great person.’); END.

9 Simple Procedure Example
PROGRAM ProcEx; VAR first_name, last_name : string[50]; PROCEDURE Message; BEGIN writeln(first_name, last_name, ‘… such a nice name.’); writeln(‘You must be a great person.’); END; { end of proc Message } BEGIN { main body } write(‘Please enter your first name -> ‘); readln(first_name); write(‘Please enter your last name -> ‘); readln(last_name); Message; END.

10 Compare The above two programs will do exactly the same thing.
Effectively we just moved two of the writeln statements of the original program into a procedure. This should illustrate a way to break large programs into smaller pieces. While this example doesn’t gain us much, keep the idea of large into small at hand.

11 Declarations Notice that in a PROGRAM all declarations of constants, variables and procedures (in that order) MUST be made before the BEGIN of the program’s body.

12 Execution Order So where does the above program start?
It starts at the first line of the program’s body: write(‘Please enter your first name ->’); The code of the procedure’s body will not be executed until the procedure is “called.”

13 Procedure Call In the above program where we have the line Message; in the program’s body… We refer to that as a Procedure Call. On that line the execution ‘jumps’ up to the procedure’s heading, executes the body of the procedure, and then returns back to where it was called from.

14 Global Variables Notice that within the procedure we referred to the variables declared in the main program’s VAR section. Variables declared in the main program’s VAR section are called global variables. Global variables MAY (usually) be referenced and changed anywhere in a program (in the main body, in procedures or in functions).

15 Global variable rules It is best to use as FEW global variables as possible. It is NOT a good idea to create procedures which alter global variables (not sent as parameters).

16 Local variables Local variables are variables declared within a procedure. For example x, y and z are local variables of the below procedure: PROCEDURE Mult3; VAR x, y, z : integer; BEGIN x := 5; y := 8; z := 12; writeln(x, ‘ * ‘, y, ‘ * ‘, z, ‘ = ‘, x*y*z); END;

17 Local variables (cont)
Local variables may only be referenced from within the procedure in which they are declared. The values of local variables get reset every time the procedure’s code is entered (the procedure is called).

18 Parameters Procedures may have parameters.
Not all procedures have parameters. Parameters have names and types and values (like variables have names and types and values). Parameters are listed in parentheses after the name of the procedure. This list of parameters is called the parameter list. For example say we wanted Mult3 to have 3 parameters…

19 Parameter example PROCEDURE Mult3( VAR x, y, z : integer); BEGIN
writeln(x, ‘ * ‘, y, ‘ * ‘, z, ‘ = ‘, x*y*z); END; We would say the above procedure has the parameters x, y, z.

20 4 types of parameters Formal variable parameters
Formal value parameters Actual variable parameters Actual value parameters Notice formal contrasts with actual and variable contrasts with value.

21 Parameters Formal vs. Actual
Formal parameters are the parameter names listed in the procedure’s declaration. So the formal parameters of Mult3 would be x, y, z. Actual parameters are names of variables used in a specific procedure call.

22 Actual parameters So pretend in the main body of a program we called the procedure Mult3 as follows, where a, b, c are global variables of type integer. BEGIN { main body } a := 2; b := 3; c := 5; Mult3(a, b, c); END. The actual parameters in the call to Mult3 would be a, b, c. Recall the formal parameters of Mult3 are still x, y, z.

23 Parameter lists Notice in the above examples the actual parameter list of Mult3 would be a, b, c. The formal parameter list of Mult3 would be x, y, z.

24 Parameter passing In the line Mult3(a, b, c)
We often say we are passing a, b and c to the procedure Mult3. This concept is referred to as parameter passing.

25 Variable parameters Notice in the heading of Mult3: PROCEDURE Mult3( VAR x, y, z : integer); There is a VAR before the x, y, z. Which means we can change the values of x, y and z within the procedure. Which means x, y, z are (formal) variable parameters. This is called passing by reference.

26 Value parameters If we had another procedure named Mult4:
PROCEDURE Mult4(m, n, o, p); BEGIN writeln(m, ‘ * ‘, n, ‘ * ‘, o, ‘ * ‘, p, ‘ = ‘, m*n*o*p); END; Notice there is no VAR in front of m, n, o and p. Which means we cannot change the values of m, n, o, and p. Which means m, n, o, p are (formal) value parameters. This is called passing by value.

27 Another main variable vs. value
So pretend in the main body of a program we called the procedures Mult3 and Mult4 as follows, where a, b, c, d, e, f, g are global variables of type integer. BEGIN { main body } a := 2; b := 3; c := 5; d := 8; e := 10; f := 11; g := 15; Mult3(a, b, c); Mult4(d, e, f, g) END.

28 Parameters of Above Main
a, b, c would be actual variable parameters in the call to Mult3. And x, y, z would be formal variable parameters of the procedure Mult3. d, e, f, g would be actual value parameters in the call to Mult4. m, n, o, p would be formal value parameters of the procedure Mult4.

29 Parameter Rule You may send constants and expressions as VALUE parameters but not as variable parameters. Example: Mult4(98, e, f, g); { will work } Mult4(a+d, e, f, g); { will work } Example: Mult3(98, b, c); { does NOT work } Mult3(a+d, b, c); { does NOT work }

30 (only variables allowed)
Summary of Parameters Variable Parameter Value Parameter VAR in formal parameter list YES NO Can be used to pass information to procedure. Can be used to pass info from the procedure back to the calling procedure or program. Expressions allowed in the actual param list. (only variables allowed)

31 Problems to try (no grade)
pages 134, 135 1, 2, 3, 4, 6, 7 page 139 9, 10, 11, 12, 14

32 Some Notes You may have procedures that have both variable and value parameters: PROCEDURE la(VAR x : real; ch : char); x is a formal variable parameter ch is a formal value parameter You may call procedures from within procedures. Be careful, you can call a procedure from within itself (see chapter 13, recursive procedures, if you are interested).

33 Conditions Procedures, like most things with bodies, have preconditions and postconditions. Preconditions are those things which are true before the procedure is called. Postconditions are those things which are true after the procedure is called.

34 Caution Notice the order of the stuff you pass to a function should correspond to the order of the formal parameters. For example consider the procedure on the next slide.

35 Caution – example PROCEDURE Sub2(x, y, z : integer); BEGIN
writeln(x – y – z ); END;

36 Caution – example main If in the main body we have:
BEGIN { main body } Sub(10, 5, 2); END We would see 3 printed to the screen since 10 – 5 – 2 = 3. Or rather x is associated with the value 10, y with the value 5 and z with the value 2.

37 Caution – example main If however we switch the order of the actual parameters. So we said: BEGIN { main body } Sub(2, 5, 10); END We would see –13 printed to the screen since 2 – 5 – 10 = -13. Or rather x is associated with the value 2, y with the value 5, and z with the value 10.

38 As you use procedures Remember the order of the actual parameters must correspond with the order of the formal parameters, or you may not get the result you thought you would.

39 Problems to try (no grade)
page , 16 pages , 19, 20, 22

40 End Procedures And thus ends Chapter 4.


Download ppt "Procedures Brent M. Dingle Texas A&M University"

Similar presentations


Ads by Google