310 likes | 441 Vues
This guide provides an overview of modules and procedures in programming, tailored for CSCE 110. It introduces the structure of modules, header declarations, and the key components necessary for defining and calling procedures. The content covers important formatting guidelines, examples for basic procedures, and the distinction between parameterized and non-parameterized procedures. Understanding these concepts is essential for developing organized and efficient code. This resource is influenced by materials from James Tam and Jennifer Welch.
E N D
Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch
Header Declarations const : Statements begin end. Where To __________ Modules Module ________ (___________ & ____________)
Procedure definition Procedures (Basic Case) Procedure call No Information Is Passed In/ No Parameters
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 *)
Calling A Procedure (Basic Case – No __________) Format: name; Example: displayInstructions; The name of the procedure is a ____________.
Where To ______ Modules It can be done ________________________ in the program – but must be done ___________ its ____________. Header Declarations const : Modules can be ______ from the ______________ of the program or from within any _______ as long as the __________ is already ___________. Module definitions Main Body begin end.
Correct First: _______ the module Second: _______ the module Important: A Module Must Be ________ Before It Can Be ___________! program exampleModule (output); procedure exampleProcedure; begin : end; begin exampleProcedure; end.
Incorrect First: ________ the module Code? Second: ____________ the module Important: A Module Must Be ________ Before It Can Be ________! program exampleModule (output); begin exampleProcedure; end. procedure exampleProcedure; begin : end;
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 *)
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 ___________ Procedure ___________
Declaring _______ Variables Format: procedure name; var <variable 1 name> : <variable 1 type>; <variable 2 name> : <variable 2 type>; : : begin : end; Example: procedure proc; var num1 : integer; num2 : integer; begin : : end;
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.
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: procedure ‘proc’ ________ variable: main module
This ________ is __________ here These 4 __________ are _________ to procedure ‘calculateInterest’ ______ 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;
pennies dimes amount quarters ___________________ To Modules • Modules generally aren’t useful unless they can ______________________. computeChange
Procedures With ____________________ Passed In Procedure call P1 P2 …Pn Procedure definition
_________Parameters 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 *)
Calling Modules (Procedures) With Parameters Format: name (Name of parameter 1, Name of parameter 2…Name of parameter n); Example: celciusToFahrenheit (celciusValue); _________Parameters
________ and _________ Parameters • __________ parameters: The parameters in the method _______________. • __________ parameters: The parameters in the method _______________. • Parameters act like _________________ within a module.
____________ 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 ____________ • _____ must be of type ____________ • _____ must be of type ____________
Procedure definition requires an __________ parameter parameters match Procedure call passes in an __________ parameter 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 __________ parameter Number of parameters not equal Procedure call passes in ________parameters 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 an __________ parameter Type mismatch Procedure call passes in a ______ parameter 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 ch : char; proc (ch); end.
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.
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 *)
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 *)
Pass by ______ vs. Pass by __________ • Each _______ parameter is ____________ to the _______ of the corresponding ____________ parameter. Called ________________________.
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 _________________.
... ... ... k a b c i j ... ... a,i b,j c,k Pass by ______ vs. Pass by __________ • 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
Module call (local variables get _________ in memory) Module ends (local variables get ____________ in memory) 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 _______________________. The program code in the module executes (the variables are used to _________________________)
q r p p p p main main main main main main calls p p calls q q returns p calls r s r r p p p main main main main r calls s s returns r returns p returns Stack Frames Example