
ef383daa13396d2b103dfdbb07730a19.ppt
- Количество слайдов: 21
Introduction to computers & programming Part Deux Abandon all hope, ye who enter here Dante, The Divine Comedy
Lecture 14 Procedures
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?
Mid semester face-off • • • Has language Remember things Basic math Can see & hear Can speak Makes decisions Has abstractions Is user friendly Can be improved YES YES YES NO WEAK NO YES YES (some things) YES (very basic) YES YES (all the time) YES (best in the world) YES (super) NO (obviously)
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?
Building blocks Procedure Procedure
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
Procedures under the microscope (this is not funny, is it? ) PROCEDURE <PROCEDURE NAME> VAR <VARIABLES> : <TYPES>; BEGIN BLA-BLA END; { of this procedure } PROCEDURE - keyword (very similar to PROGRAM) The rest is just like a program.
Enough already, lets see it! Program Way. Too. Cool. Procedure. Demo; { Notice no begin } procedure Address; begin writeln( ‘Joe Bloggs’ ); writeln( ‘Hackers Road, 256, Apt #01’ ); writeln( ‘Bugtown, AZ, 01256’ ); end; VAR s. Dummy : String[ 30 ]; { now begin } begin writeln( ‘Hello I am Joe, what’’s your name? ’ ); readln( s. Dummy ); writeln( ‘Nice to meet you. I live at: ’); { Pay close attention } Address; writeln( ‘What do you think about stock market? ’ ); readln( s. Dummy ); writeln( ‘I agree. I seem to know a lot. Stop by: ‘); Address; end.
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 i. Count, i. Num. Labels : integer; c. Trick : char; { now begin } begin writeln( ‘How many labels would you like? ’ ); readln( i. Num. Labels ); for i. Count : = 1 to i. Num. Labels do begin writeln; Address; writeln; end; writeln( ‘Press enter to exit the program’ ); read( c. Trick ); end.
What does it mean to execute procedure from computer perspective? program My. Program; procedure Proc 1; begin writeln( ‘Hello cruel world’); end; procedure Proc 2 begin writeln( ‘Good bye cruel world’); end; begin Proc 1; Proc 2; end. Program My. Program; begin { Proc 1 gets substituted with } begin writeln( ‘Hello cruel world’ ); end; { Proc 2 gets substituted with } begin writeln( ‘Good bye cruel world’ ); end.
Local variables program Prelude. To. Trouble; procedure With. Local. Variable var i. One. Poor. Integer : integer; Creates box in memory i. One. Poor. Integer lives here begin i. One. Poor. Integer : = 20; writeln( i. One. Poor. Integer ); end; { end of With. Local. Variable } { begin on of main } begin i. One. Poor. Integer : = 20; writeln( i. One. Poor. Integer ); end; Compiler error !!!
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! VARIABLE = Diary ACCESS ALLOWED YOU = Procedure OF COURSE LANDLORD = MAIN PROGRAM NO WAY
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.
Local vs. global variable Definition Global variable is the one declared outside of any procedure 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? )
Example of visibility of global variables Program Global. Vars. Demo; var i. Global. Everyone. Sees. Me : integer; procedure proc 1; var i. Local. One : integer; begin writeln( i. Local. One ); writeln(i. Global. Everyone. Sees. Me ); writeln(i. Global. But. Proc 1 Doesnt. See. Me); end; var i. Global. But. Proc 1 Doesnt. See. Me : integer; begin writeln(i. Global. Everyone. Sees. Me ); writeln(i. Global. But. Proc 1 Doesnt. See. Me ); end. Ok Ok Error Ok Ok
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 Local Local Local Global Global
Here is a tricky part • What if we declare two variable one local one global with THE SAME NAME? ? ?
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 3 x 3 square using ‘*’ character. The program should read in the number of squares to be printed
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 Proc. A as follows: ********** * * * * where procedure Horiz. Line would produce *****; and Vert. Lines, would produce: * * * * Thus Proc. A would be written as: Proc. A; BEGIN Horiz. Line; Vert. Lines; Horiz. Line; Vert. Lines END; On the other hand, the letter "H" would be produced by the following sequence: Vertlines; Horizline; Vert. Line. 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.
Programs for this lecture • Address • Labels • Global vs. local
ef383daa13396d2b103dfdbb07730a19.ppt