modules the basics n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Modules – the Basics PowerPoint Presentation
Download Presentation
Modules – the Basics

Loading in 2 Seconds...

play fullscreen
1 / 31

Modules – the Basics - PowerPoint PPT Presentation


  • 92 Views
  • Uploaded on

Modules – the Basics. CSCE 110 Influenced by material developed by James Tam & Jennifer Welch. Header. Declarations. const :. Statements. begin end. Where To Define Modules. Module definitions ( procedures & functions ). Procedure definition. Procedures (Basic Case).

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Modules – the Basics' - soo


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
modules the basics

Modules – the Basics

CSCE 110

Influenced by material developed by James Tam & Jennifer Welch

where to define modules

Header

Declarations

const

:

Statements

begin

end.

Where To Define Modules

Module definitions (procedures & functions)

procedures basic case

Procedure definition

Procedures (Basic Case)

Procedure call

No Information Is Passed In/ No Parameters

defining procedures basic case no parameters
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 *)

calling a procedure basic case no parameters
Calling A Procedure (Basic Case – No Parameters)

Format:

name;

Example:

displayInstructions;

The name of the procedure is a statement.

where to call modules
Where To Call Modules

It can be done most anywhere in the program – but must be done after its definition.

Header

Declarations

const

:

Modules can be called from the main body of the program or from within any module as long as the module is already defined.

Module definitions

Main Body

begin

end.

important a module must be defined before it can be called

Correct

First: Defining the module

Second: Calling the module

Important: A Module Must Be Defined Before It Can Be Called!

program exampleModule (output);

procedure exampleProcedure;

begin

:

end;

begin

exampleProcedure;

end.

important a module must be defined before it can be called1

Incorrect

First: Calling the module

Code?

Second: Defining the module

Important: A Module Must Be Defined Before It Can Be Called!

program exampleModule (output);

begin

exampleProcedure;

end.

procedure exampleProcedure;

begin

:

end;

procedures
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 *)

procedures1
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

declaring local variables
Declaring Local 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 local variables1
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.

declaring local variables2
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: procedure ‘proc’

Local variable: main module

local variables have limited scope

This variable is unknown here

These 4 variables are local to procedure ‘calculateInterest’

Local Variables Have LimitedScope

procedure getInformation;

begin

write (‘Enter the principle: ‘);

readln (principle);

end;

procedure calculateInterest;

var

amount : integer;

principle : integer;

interest : integer;

time : integer;

begin

getInformation;

end;

passing information to modules

pennies

dimes

amount

quarters

Passing Information To Modules
  • Modules generally aren’t useful unless they can pass information.

computeChange

procedures with parameters information passed in
Procedures With Parameters/Information Passed In

Procedure call

P1

P2

…Pn

Procedure definition

defining modules procedures with parameters

FormalParameters

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

calling modules procedures with parameters
Calling Modules (Procedures) With Parameters

Format:

name (Name of parameter 1, Name of parameter 2…Name of parameter n);

Example:

celciusToFahrenheit (celciusValue);

ActualParameters

formal and actual parameters
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.
formal and actual parameters1
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
important the formal and actual parameter lists must match

Procedure definition requires an integer parameter

parameters match

Procedure call passes in an integer parameter

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.

important the formal and actual parameter lists must match1

Procedure definition requires one integer parameter

Number of parameters

not equal

Procedure call passes in zeroparameters

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.

important the formal and actual parameter lists must match2

Procedure definition requires an integer parameter

Type mismatch

Procedure call passes in a char parameter

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 ch : char;

proc (ch);

end.

example problem
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
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
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 value vs pass by reference
Pass by Value vs. Pass by Reference
  • Each formal parameter is initialized to the value of the corresponding actual parameter. Called passing parameters.
pass by value vs pass by reference1
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’slocation are copied to formP’slocation
      • 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.
pass by value vs pass by reference2

...

...

...

k

a

b

c

i

j

...

...

a,i

b,j

c,k

Pass by Value vs. Pass by Reference
  • 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
stack frames

Module call (local variables get allocated in memory)

Module ends (local variables get de-allocated in memory)

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.

The program code in the module executes (the variables are used to store information for the module)

stack frames example

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