Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15 Variable passing Appearances are often deceiving Aesop, Fables.

Similar presentations


Presentation on theme: "Lecture 15 Variable passing Appearances are often deceiving Aesop, Fables."— Presentation transcript:

1 Lecture 15 Variable passing Appearances are often deceiving Aesop, Fables

2 Last lecture review Procedure is a building block Program consists of several of procedures Procedure looks like a small program Procedures are declared using keyword ‘PROCEDURE’ Procedure has its own scope, that is variables declared in the procedure are accessible within procedure only Global variables declared before procedure are accessible in this procedure Global variables are always accessible in the main program

3 Optional program from last time Write program which has procedure square. Procedure Square prints 3x3 square using ‘*’ character. The program should read in the number of squares to be printed

4 Optional program from last time program SquareViaProcedure; procedure Square; var iLoopCount : integer; begin for iLoopCount := 1 to 3 do begin writeln('***'); end; end; { of procedure Square } { Main program } var iNumSquares, iSquareCount : integer; cDummy : char; begin write( 'Please enter the number of squares: ' ); readln( iNumSquares ); for iSquareCount := 1 to iNumSquares do begin writeln; Square; writeln; end; writeln( 'Press enter to exit...' ); read( cDummy ); end.

5 What’s wrong with this? procedure FilledLine; begin writeln( ‘********’ ); end;

6 But what if we want to print longer or shorter lines????? We need some way to specify that length of line is variable. Wait a minute, you said variable?

7 Parameters to the rescue procedure FilledLine( iLineLength : integer ) var iCount : integer; begin for iCount := 1 to iLineLength do begin write( ‘*’ ); end; writeln; end; Procedure parameter is like a regular variable. It is passed into procedure and it has its own box.

8 Fine but how do we call this procedure? Like this: FilledLine( 10 ) ---> ********** FilledLine( 5 ) ---> ***** FilledLine( 0 ) --->

9 Similarly ShallowLine procedure ShallowLine( iLineLength : integer ) begin writeln( ‘*’,’*’:(iLineLength - 1 )); end; ShallowLine( 10 ) ---> * * ShallowLine( 5 ) ---> * * ShallowLine( 0 ) --->

10 Putting it together Program ProcsWithParameters;... procedure DoSomething( iMyInteger : integer ); begin … end; { Main program } begin … DoSomething( 123 );... DoSomething( -567 ); … DoSomething( 1.234 ); end. Compiler error Types mismatch!

11 Big step toward hw 4 The ‘Boo’ program It is a program, which prints B O

12 Top-down design Where do I start? Ok, main should probably call some procedure(s), right? Lets see. I am thinking in pseudo code print B print O With main program Sure. Think about what the program should do. Very good. So which procedures do you think we would need?

13 Top-down design That’s easy letterB; letterO; Sure. Now we need to refine what every procedure does. BTW I think its really cool that letterO is a procedure, don’t you? (we can just call it twice to print 2 letter ‘O’ Great, so the main would look like this: { main } begin letterB; letterO; end.

14 Top-down design Hmm… Before? You mean like for-loops and built-in functions? Yeah, it’s way cool. Okay, lets work on letterB procedure. Remember to use things that we’ve learned before. Yes. Definitely those. Also use procedures that we created before.

15 Top-down design Oh, I see, you mean like FilledLine and ShallowLine? Now I understand. This is very cool, I can just think in terms of procedures that we wrote before. I don’t need to bother with how they work! Exactly! They are our building blocks. Instead of starting from scratch just think of creating letter ‘B’ using these two procedure.

16 Letter ‘B’ under the microscope FilledLine( 10 ) ShallowLine( 11 )

17 I got it! Procedure LetterB; begin FilledLine( 10 ); ShallowLine( 11 ); FilledLine( 10 ); ShallowLine( 11 ); FilledLine( 10 ); end; How about this? Flawless. I am proud of you kid!

18 Now procedure to print ‘O’ Procedure LetterO; begin FilledLine( 10 ); ShallowLine( 10 ); FilledLine( 10 ); end; Note that we called ShallowLine with 10 here!

19 Now the whole thing! procedure ClearScreen; VAR iCount : integer; begin for iCount := 1 to 50 do writeln; end; { Clear screen } procedure FilledLine( iLength : integer ); VAR iCount : integer; begin for iCount := 1 to iLength do begin write( '*' ); end; writeln; end; { of procedure FilledLine } procedure ShallowLine( iLength : integer ); begin writeln('*','*':(iLength - 1)); end; { of procedure FilledLine } procedure LetterB; begin FilledLine( 10 ); ShallowLine( 11 ); FilledLine( 10 ); ShallowLine( 11 ); FilledLine( 10 ); end; { end of procedure LetterB } procedure LetterO; begin FilledLine( 11 ); ShallowLine( 11 ); FilledLine( 11 ); end; { of procedure LetterO } { Main program } begin ClearScreen; LetterB; LetterO; end.

20 Now what? Am I the master of procedures now? You are almost there. There is one more really nasty, but powerful concept

21 What about the value of a variable passed into procedure? Suppose we call a procedure with a variable and procedure assigns some new value to this variable. What happens? Procedure Test( iSomeInteger : integer ); begin … iSomeInteger := 10; … end; var iGlobalInteger : integer; begin … iGlobalInteger = 9; Test( iGlobalInteger ); end.

22 Optional problem for next time Write a program which reads the number of students’ homework and then loops, prompting user for next grade. Once grade is entered, program outputs corresponding number of stars using FilledLine procedure. This program is simpler than the one discussed in 6.3

23 Homework Homework 4 is due Monday, November 9 Prepare for a little quiz on 6.1 - 6.3

24 Programs from this lecture Square BOO Pass by value


Download ppt "Lecture 15 Variable passing Appearances are often deceiving Aesop, Fables."

Similar presentations


Ads by Google