J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.

Slides:



Advertisements
Similar presentations
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Advertisements

J. Michael Moore Text Files CSCE 110 From James Tam’s material.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables in Pascal.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller parts that are easier to implement and.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in your Pascal programs.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
J. Michael Moore Scope & Testing Modules CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
J. Michael Moore Arrays CSCE 110 From James Tam’s material.
J. Michael Moore Modules: Getting information out CPSC 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
J. Michael Moore Input and Output (IO) CSCE 110 Drawn from James Tam's material.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
J. Michael Moore Software Design CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
Getting Started With Pascal Programming
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
James Tam Introduction To Files In Pascal You will learn how to read from and write to text files in Pascal.
J. Michael Moore Arrays CSCE 110. J. Michael Moore Typical (although simplified) Problem Write a program that will track student grades in a class. The.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
Chapter 6: User-Defined Functions I
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
James Tam Pointers In this section of notes you will learn about a third type of variable that stores addresses rather than data.
J. Michael Moore Scope & Testing Modules CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to text files in Pascal.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Problem Decomposition This section of notes shows you how to break down a large problem into smaller parts that are easier to implement and manage.
J. Michael Moore Recursion CSCE 110 From James Tam’s material.
Chapter 6: Functions.
Functions.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
Introduction to Pascal The Basics of Program writing.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions + Overloading + Scope
Chapter 7: User-Defined Functions II
Implementing Subprograms
Principles of programming languages 4: Parameter passing, Scope rules
Functions, Part 2 of 2 Topics Functions That Return a Value
Anatomy of a Function Part 3
How Functions Work Part 1
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
CS 432: Compiler Construction Lecture 11
Breaking Problems Down
Presentation transcript:

J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch

J. Michael Moore Where To __________ Modules Header const : Declarations begin end. Statements Module ________ (___________ & ____________)

J. Michael Moore Procedures (Basic Case) Procedure call Procedure definition No Information Is Passed In / No Parameters

J. Michael Moore Defining Procedures (Basic Case – No __________) Format: procedure name; begin { Statements of the procedure go here } end; { End of procedure name } Example: procedure displayInstructions; begin writeln ('The statements in this module will'); writeln (' typically give a high level'); writeln (' overview of what the program as a'); writeln ('whole does'); end; (* End of procedure displayInstructions *)

J. Michael Moore Calling A Procedure (Basic Case – No __________) Format: name; Example: displayInstructions; The name of the procedure is a ____________.

J. Michael Moore Where To ______ Modules It can be done ________________________ in the program – but must be done ___________ its ____________. Header const : Declarations begin end. Main Body Module definitions Modules can be ______ from the ______________ of the program or from within any _______ as long as the __________ is already ___________.

J. Michael Moore Important: A Module Must Be ________ Before It Can Be ___________! program exampleModule (output); procedure exampleProcedure; begin : end; begin exampleProcedure; end. First: _______ the module Second: _______ the module Correct

J. Michael Moore Important: A Module Must Be ________ Before It Can Be ________! program exampleModule (output); begin exampleProcedure; end. procedure exampleProcedure; begin : end; Second: ____________ the module First: ________ the module Code? Incorrect

J. Michael Moore Procedures firstExampleProcedure.pas program firstExampleProcedure (output); procedure displayInstructions; begin writeln ('The statements in this module will typically give a’); writeln (‘high level overview of what the program as a’); writeln (‘whole does'); end; (*Procedure displayInstructions *) begin displayInstructions; writeln('Thank you, come again!'); end. (* Program *)

J. Michael Moore Procedures firstExampleProcedure.pas program firstExampleProcedure (output); procedure displayInstructions; begin writeln ('The statements in this module will typically give a’); writeln (‘high level overview of what the program as a’); writeln (‘whole does'); end; (*Procedure displayInstructions *) begin displayInstructions; writeln('Thank you, come again!'); end. (* Program *) Procedure ___________

J. Michael Moore Declaring _______ Variables Format: procedure name; var : ; : : begin : end; Example: procedure proc; var num1 : integer; num2 : integer; begin : : end;

J. Michael Moore Declaring __________ Variables program secondExampleProcedure (output); procedure proc; var num1 : integer; begin var num2 : integer; num1 := 1; num2 := 2; writeln(num1, ' ', num2); end; begin var num1 : integer; num1 := 10; writeln(num1); proc; writeln(num1); end.

J. Michael Moore Declaring ________ Variables secondExampleProcedure.pas program secondExampleProcedure (output); procedure proc; var num1 : integer; begin var num2 : integer; num1 := 1; num2 := 2; writeln(num1, ' ', num2); end; begin var num1 : integer; num1 := 10; writeln(num1); proc; writeln(num1); end. ________ variable: main module ______ variable: procedure ‘proc’

J. Michael Moore ______ Variables Have ____________________ procedure getInformation; begin write (‘Enter the principle: ‘); readln (principle); end; procedure calculateInterest; var amount: integer; principle: integer; interest: integer; time: integer; begin getInformation; end; These 4 __________ are _________ to procedure ‘calculateInterest’ This ________ is __________ here

J. Michael Moore ___________________ To Modules Modules generally aren’t useful unless they can ______________________. computeChange amount quarters dimes pennies

J. Michael Moore Procedures With ____________________ Passed In Procedure call P1P1 P2P2 …P n Procedure definition

J. Michael Moore Defining Modules (Procedures) With ____________ Format: procedure name (Name of parameter 1 : type of parameter 1; Name of parameter 2 : type of parameter 2; : Name of parameter n : type of parameter n); begin (* Statements of the procedure go here *) end; Example: procedure celciusToFahrenheit (celciusValue : real); var fahrenheitValue : real; begin fahrenheitValue := 9 / 5 * celciusValue + 32; writeln(‘temperature in Celsius: ', celciusValue:0:2); writeln(‘temperature in Fahrenheit: ', fahrenheitValue:0:2); end; (* Procedure celciusToFahrenheit *) _________ Parameters

J. Michael Moore Calling Modules (Procedures) With Parameters Format: name (Name of parameter 1, Name of parameter 2…Name of parameter n); Example: celciusToFahrenheit (celciusValue); _________ Parameters

J. Michael Moore ________ and _________ Parameters __________ parameters: The parameters in the method _______________. Parameters act like _________________ within a module.

J. Michael Moore ____________ and ______________ Parameters Module call and module definition must have: _____________ of parameters ______________ parameters must have the same _______ e.g. definition: procedure calc(i: integer, f: real, b:boolean); e.g. call: calc(num, avg, flag) where: —_____ must be of type ____________

J. Michael Moore Important: The _____ and _____ Parameter Lists Must _____! The ______ and _________ of parameters must ________ or there will be a compilation error. program parameterExample; procedure proc (num : integer); begin num := 10; end; begin var num : integer; proc (num); end. Procedure definition requires an __________ parameter Procedure call passes in an __________ parameter parameters match

J. Michael Moore Important: The _____ and _____ Parameter Lists Must _____! The ______ and _________ of parameters must ________ or there will be a compilation error. program parameterExample; procedure proc (num : integer); begin num := 10; end; begin proc; end. Procedure definition requires __________ parameter Procedure call passes in ________ parameters Number of parameters not equal

J. Michael Moore The ______ and _________ of parameters must ________ or there will be a compilation error.. program parameterExample; procedure proc (num : integer); begin num := 10; end; begin var ch : char; proc (ch); end. Procedure definition requires an __________ parameter Procedure call passes in a ______ parameter Type mismatch Important: The _____ and _____ Parameter Lists Must _____!

J. Michael Moore Example Problem Write a program that will convert a temperature value from Celsius to Fahrenheit. The part of the program that performs that actual conversion should take the form of a separate module.

J. Michael Moore Procedures: Putting Together The Case Of Procedures With Parameters temperatureConverter.pas program temperatureConverter (input, output); procedure celsiusToFahrenheit (celsiusValue : real); var fahrenheitValue : real; begin fahrenheitValue := 9 / 5 * celsiusValue + 32; writeln('Temperature in Celsius: ', celsiusValue:0:2); writeln('Temperature in Fahrenheit: ', fahrenheitValue:0:2); end; (* Procedure celsiusToFahrenheit *)

J. Michael Moore Procedures: Putting Together The Case Of Procedures With Parameters (2) begin var celsiusValue : real; writeln; writeln('This program will convert a given temperature from a Celsius'); writeln('value to a Fahrenheit value.'); write(‘Enter a temperature in Celsius: '); readln(celsiusValue); writeln; celsiusToFahrenheit(celsiusValue); writeln('Thank you and come again.'); end. (* Program *)

J. Michael Moore Pass by ______ vs. Pass by __________ Each _______ parameter is ____________ to the _______ of the corresponding ____________ parameter. Called ________________________.

J. Michael Moore Pass by ______ vs. Pass by __________ Let actP be actual and formP be formal Pass by __________ —actP and formP refer to __________ memory locations —________ of actP ’ s ______________ to formP ’ s _______ —changes to formP are __________ to the caller (i.e. actP) Pass by Reference —actP and formP refer to __________ memory location —the _______ for formP is ____________________ for actP —changes to formP are __________ to the caller (i.e. actP) By default, Pascal passes variables by ________. However, we’ll see how to make it pass by _________________.

J. Michael Moore procedure larger(i:integer; j:integer; k:integer); begin if (i<j) then i := j; k := i; end; Suppose that larger is called with: a := 3; b := 5; larger(a,b,c); After calling larger and passing by __________, then a = 3, b = 5, c = 5 After calling larger if passing by __________, then a = 5, b = 5, c = 5 Pass by ______ vs. Pass by __________... a,ib,jc,k... abcji k

J. Michael Moore Stack Frames When a method begins executing, space on the ________, called a _____________, is ____________ for it, to hold formal parameters local variables declared in the method return value (for functions) When the method finishes __________, the _______ frame is ______________, and the formal parameters and local variables are _______________________. Module call (local variables get _________ in memory) The program code in the module executes (the variables are used to _________________________) Module ends (local variables get ____________ in memory)

J. Michael Moore Stack Frames Example main p main calls p p main q p calls q p main q returns p main r p calls r p main r r calls s s p main r s returns p main r returns main p returns