Introduction to computers PowerPoint PPT Presentation

presentation player overlay
1 / 21
About This Presentation
Transcript and Presenter's Notes

Title: Introduction to computers


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

2
Lecture 14
  • Procedures

3
Whats 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 cant we do?

4
Mid semester face-off
  • Has language YES YES
  • Remember things YES YES (some things)
  • Basic math YES YES (very basic)
  • Can see hear YES YES
  • Can speak YES YES
  • Makes decisions NO YES (all the time)
  • Has abstractions WEAK YES (best in the world)
  • Is user friendly NO YES (super)
  • Can be improved YES NO (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
Procedure
Procedure
Procedure
Procedure
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 weve
    acquired so far variables, for-loops, writeln etc

8
Procedures under the microscope(this is not
funny, is it?)
PROCEDURE ltPROCEDURE NAMEgt VAR ltVARIABLESgt
ltTYPESgt 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, whats 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 )

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

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 doesnt
    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

14
But you can see your dads 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.

Ok
Ok
Error
Ok
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
Global
Local
Global
Local
Global
Local
Global
Local
Global
Local
Global
Local
Local
Local
Local
Local
Local
Local
Local
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 lettersA, 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

21
Programs for this lecture
  • Address
  • Labels
  • Global vs. local
Write a Comment
User Comments (0)
About PowerShow.com