Procedures Brent M. Dingle Texas A&M University

Slides:



Advertisements
Similar presentations
Sub Programs To Solve a Problem, First Make It Simpler.
Advertisements

CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CS0004: Introduction to Programming Subprocedures and Modular Design.
Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part D (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
JavaScript, Fourth Edition
Loops Brent M. Dingle Texas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Pascal Programming Today Chapter 11 1 Chapter 11.
Top Down Design Brent M. Dingle Texas A&M University Chapter 4 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
A Simple Program CPSC Pascal Brent M. Dingle Texas A&M University 2001, 2002.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Fall 2001(c)opyright Brent M. Dingle 2001 Multidimensional Arrays Brent M. Dingle Texas A&M University Chapter 10 – Section 2, part B (and some from Mastering.
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.
Implementing Subprograms
Chapter 2 – part b Brent M. Dingle Texas A&M University
Chapter 9: Value-Returning Functions
Structured Programming
Implementing Subprograms Chapter 10
User-Written Functions
Topic: Recursion – Part 2
Implementing Subprograms
Chapter 7: User-Defined Functions II
Chapter 5 Functions for All Subtasks 1
The CONST definition CONST Pi = , City = ‘New York’;
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
Implementing Subprograms
Chapter 10: Void Functions
Principles of programming languages 4: Parameter passing, Scope rules
JavaScript: Functions
Chapter 15: Overloading and Templates
Chapter 4: Writing Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
(c)opyright Brent M. Dingle 2001
Implementing Subprograms
User Defined Functions
Structured Programming
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Brent M. Dingle Texas A&M University Chapter 12 – section 1
Chapter 4 Writing Classes.
PROGRAMMING Sub Procedures I.
Functions Pass By Value Pass by Reference
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Turbo Pascal Units (TPU)
Defining Classes and Methods
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Defining Classes and Methods
Course Overview PART I: overview material PART II: inside a compiler
Simple Branches and Loops
(c)opyright Brent M. Dingle 2001
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Complex Array Structures
Brent M. Dingle Texas A&M University Chapter 5 – Section 1
Implementing Subprograms
Presentation transcript:

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)

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.

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.

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

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

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.

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. =)

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.

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.

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.

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.

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.”

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.

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).

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).

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;

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).

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…

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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 }

(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)

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

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).

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.

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.

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

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.

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.

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.

Problems to try (no grade) page 152 15, 16 pages 155 - 156 18, 19, 20, 22

End Procedures And thus ends Chapter 4.