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

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
J. Michael Moore Text Files CSCE 110 From James Tam’s material.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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.
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.
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 What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
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.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
Run-Time Storage Organization
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.
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 Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
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.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
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 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.
Chapter 6: Functions.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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.
© 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.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Introduction to Pascal The Basics of Program writing.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
User-Defined Functions II TK1914: C++ Programming.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Functions + Overloading + Scope
Implementing Subprograms Chapter 10
Chapter 7: User-Defined Functions II
Implementing Subprograms
Principles of programming languages 4: Parameter passing, Scope rules
User-Defined Functions
Anatomy of a Function Part 3
How Functions Work Part 1
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Chapter 7: User-Defined Functions II
CS 432: Compiler Construction Lecture 11
Breaking Problems Down
Corresponds with Chapter 5
Presentation transcript:

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

J. Michael Moore Where To Define Modules Header const : Declarations begin end. Statements Module definitions (procedures & functions)

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 Parameters) 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 Parameters) Format: name; Example: displayInstructions; The name of the procedure is a statement.

J. Michael Moore Where To Call Modules It can be done most anywhere in the program – but must be done after its definition. Header const : Declarations begin end. Main Body Module definitions Modules can be called from the main body of the program or from within any module as long as the module is already defined.

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

J. Michael Moore Important: A Module Must Be Defined Before It Can Be Called! program exampleModule (output); begin exampleProcedure; end. procedure exampleProcedure; begin : end; Second: Defining the module First: Calling 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 definition Procedure call

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

J. Michael Moore Declaring Local 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 Local 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. Local variable: main module Local variable: procedure ‘proc’

J. Michael Moore Local Variables Have Limited Scope 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 variables are local to procedure ‘calculateInterest’ This variable is unknown here

J. Michael Moore Passing Information To Modules Modules generally aren’t useful unless they can pass information. computeChange amount quarters dimes pennies

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

J. Michael Moore Defining Modules (Procedures) With Parameters 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 *) Formal 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); Actual Parameters

J. Michael Moore Formal and Actual Parameters Formal parameters: The parameters in the module definition. Actual parameters: The parameters in the module call. Parameters act like local variables within a module.

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

J. Michael Moore Important: The Formal and Actual Parameter Lists Must Match! The type and number of parameters must match 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 integer parameter Procedure call passes in an integer parameter parameters match

J. Michael Moore Important: The Formal and Actual Parameter Lists Must Match! The type and number of parameters must match or there will be a compilation error. program parameterExample; procedure proc (num : integer); begin num := 10; end; begin proc; end. Procedure definition requires one integer parameter Procedure call passes in zero parameters Number of parameters not equal

J. Michael Moore The type and number of parameters must match 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 integer parameter Procedure call passes in a char parameter Type mismatch Important: The Formal and Actual Parameter Lists Must Match!

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 Value vs. Pass by Reference Each formal parameter is initialized to the value of the corresponding actual parameter. Called passing parameters.

J. Michael Moore Pass by Value vs. Pass by Reference Let actP be actual and formP be formal Pass by Value —actP and formP refer to different memory locations —contents of actP ’ s location are copied to formP ’ s location —changes to formP are invisible to the caller (i.e. actP) Pass by Reference —actP and formP refer to the same memory location —the address for formP is equated with the address for actP —changes to formP are visible to the caller (i.e. actP) By default, Pascal passes variables by value. However, we’ll see how to make it pass by reference.

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 value, then a = 3, b = 5, c = ? After calling larger if passing by reference, then a = 5, b = 5, c = 5 Pass by Value vs. Pass by Reference... a,ib,jc,k... abcji k

J. Michael Moore Stack Frames When a module begins executing, space on the stack, called a stack frame, is allocated for it, to hold formal parameters local variables declared in the method return value (for functions) When the method finishes executing, the stack frame is de-allocated, and the formal parameters and local variables are no longer accessible. Module call (local variables get allocated in memory) The program code in the module executes (the variables are used to store information for the module) Module ends (local variables get de-allocated 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