Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to computers & programming Part Deux Abandon all hope, ye who enter here Dante, The Divine Comedy.

Similar presentations

Presentation on theme: "Introduction to computers & programming Part Deux Abandon all hope, ye who enter here Dante, The Divine Comedy."— Presentation transcript:

1 Introduction to computers & programming Part Deux Abandon all hope, ye who enter here Dante, The Divine Comedy

2 Lecture 14 Procedures

3 What’s next? We have elementary tools What stops us from writing large programs? What additional features would we want? What are some of the problems that we can run into with current tools? What kind of things can’t we do?

4 Mid semester face-off Has languageYESYES Remember thingsYESYES (some things) Basic mathYESYES (very basic) Can see & hearYESYES Can speakYESYES Makes decisionsNOYES (all the time) Has abstractionsWEAKYES (best in the world) Is user friendlyNOYES (super) Can be improvedYESNO (obviously)

5 What makes us what we are? Automatic tasks Task as abstraction Humans as manipulators of abstractions Abstractions as building blocks Perhaps we should make programs out of building blocks?

6 Building blocks Procedure

7 Procedure (boring facts) Procedure is like a little program Accomplishes definite tasks Is a building block of a program Can be called from anywhere in the program (including other procedures) Has structure similar to a program Allows us to use all the tools that we’ve acquired so far: variables, for-loops, writeln etc

8 Procedures under the microscope (this is not funny, is it?) PROCEDURE VAR : ; BEGIN BLA-BLA-BLA END; { of this procedure } PROCEDURE - keyword (very similar to PROGRAM) The rest is just like a program.

9 Enough already, lets see it! Program WayTooCoolProcedureDemo; { Notice no begin } procedure Address; begin writeln( ‘Joe Bloggs’ ); writeln( ‘Hackers Road, 256, Apt #01’ ); writeln( ‘Bugtown, AZ, 01256’ ); end; VAR sDummy : String[ 30 ]; { now begin } begin writeln( ‘Hello I am Joe, what’’s your name?’ ); readln( sDummy ); writeln( ‘Nice to meet you. I live at:’); { Pay close attention } Address; writeln( ‘What do you think about stock market?’ ); readln( sDummy ); writeln( ‘I agree. I seem to know a lot. Stop by: ‘); Address; end.

10 Lets make this more useful Program Labels; procedure Address; begin writeln( chr(1), ‘Alex Iskold’, chr(1) ); writeln( ‘456 Park Ave, Apt 312’ ); writeln( ‘New York, NY, 10123’ ); end; var iCount, iNumLabels : integer; cTrick : char; { now begin } begin writeln( ‘How many labels would you like?’ ); readln( iNumLabels ); for iCount := 1 to iNumLabels do begin writeln; Address; writeln; end; writeln( ‘Press enter to exit the program’ ); read( cTrick ); end.

11 What does it mean to execute procedure from computer perspective? program MyProgram; procedure Proc1; begin writeln( ‘Hello cruel world’); end; procedure Proc2 begin writeln( ‘Good bye cruel world’); end; begin Proc1; Proc2; end. Program MyProgram; begin { Proc1 gets substituted with } begin writeln( ‘Hello cruel world’ ); end; { Proc2 gets substituted with } begin writeln( ‘Good bye cruel world’ ); end; end.

12 Local variables program PreludeToTrouble; procedure WithLocalVariable var iOnePoorInteger : integer; begin iOnePoorInteger := 20; writeln( iOnePoorInteger ); end; { end of WithLocalVariable } { begin on of main } begin iOnePoorInteger := 20; writeln( iOnePoorInteger ); end; Creates box in memory iOnePoorInteger lives here Compiler error !!!

13 What was wrong??? Just because your dad owns your house it doesn’t mean that he can walk into your room and look at your diary! V ARIABLE = Diary ACCESS ALLOWED YOU = Procedure OF COURSE LANDLORD = MAIN PROGRAM NO WAY

14 But you can see your dad’s diary? Yes! Here is why: You are suppose to know more than your parents! For example, you are quite familiar with new technology + old technology, but your parents (most likely) know less about it.

15 Local vs. global variable Definition Global variable is the one declared outside of any procedure Definition Local variable is the variable declared inside some procedure Local variables can only be used in inside procedure Global variables are visible in the main program and any procedure BEFORE which they were declared (huh?)

16 Example of visibility of global variables Program GlobalVarsDemo; var iGlobalEveryoneSeesMe : integer; procedure proc1; var iLocalOne : integer; begin writeln( iLocalOne ); writeln(iGlobalEveryoneSeesMe ); writeln(iGlobalButProc1DoesntSeeMe); end; var iGlobalButProc1DoesntSeeMe : integer; begin writeln(iGlobalEveryoneSeesMe ); writeln(iGlobalButProc1DoesntSeeMe ); end. Error Ok

17 In terms of boxes in memory EVERY variable (global or local) has its separate box in memory If you declare 5 global variable and 15 local, there are total of 20 variables in your program Local Global Local Global

18 Here is a tricky part What if we declare two variable one local one global with THE SAME NAME???

19 Home work Read chapter 6 (Sections 1-3) Homework 4 is due Monday, November 9 Optional exercise 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

20 Write a program that produces block letters for any six of the following letters:A, B, C, E, F, G, H, J, L, O, P, S, or U. You should choose the six letters you want printed by writing the appropriate procedures calls in your main program. For example, the letter A would be formed by procedure ProcA as follows: ********** * ********** * where procedure HorizLine would produce **********; and VertLines, would produce: * Thus ProcA would be written as: ProcA; BEGIN HorizLine; VertLines; HorizLine; VertLines END; On the other hand, the letter "H" would be produced by the following sequence: Vertlines; Horizline; VertLine. By writing an additional procedure that produces a vertical line in column 1, and another one that produces a vertical line in column 10, your program can produce all of the indicated letters. All the letters should be the approximately the same height.

21 Programs for this lecture Address Labels Global vs. local

Download ppt "Introduction to computers & programming Part Deux Abandon all hope, ye who enter here Dante, The Divine Comedy."

Similar presentations

Ads by Google