Top Banner
4 2 5 1 0011 0010 1010 1101 0001 0100 1011 Introduction to computers & programming Part Deux Abandon all hope, ye who enter here Dante, The Divine Comedy
21

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

Dec 16, 2015

Download

Documents

Geoffrey Bishop
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to computers & programming Part Deux Abandon all hope, ye who enter here Dante, The Divine Comedy.

42510011 0010 1010 1101 0001 0100 1011

Introduction to computers & programming

Part Deux

Abandon all hope, ye who enter here

Dante, The Divine Comedy

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

42510011 0010 1010 1101 0001 0100 1011

Lecture 14

Procedures

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

4251

0011 0010 1010 1101 0001 0100 1011

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?

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

4251

0011 0010 1010 1101 0001 0100 1011

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)

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

4251

0011 0010 1010 1101 0001 0100 1011

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?

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

4251

0011 0010 1010 1101 0001 0100 1011

Building blocks

ProcedureProcedure

Procedure

Procedure

Procedure

Procedure

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

4251

0011 0010 1010 1101 0001 0100 1011

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

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

4251

0011 0010 1010 1101 0001 0100 1011

Procedures under the microscope(this is not funny, is it?)

PROCEDURE <PROCEDURE NAME>

VAR <VARIABLES> : <TYPES>;

BEGIN

BLA-BLA-BLA

END; { of this procedure }

PROCEDURE - keyword (very similar to PROGRAM)

The rest is just like a program.

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

4251

0011 0010 1010 1101 0001 0100 1011

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.

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

4251

0011 0010 1010 1101 0001 0100 1011

Lets make this more usefulProgram 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.

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

4251

0011 0010 1010 1101 0001 0100 1011

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.

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

4251

0011 0010 1010 1101 0001 0100 1011

Local variablesprogram 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 !!!

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

4251

0011 0010 1010 1101 0001 0100 1011

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

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

4251

0011 0010 1010 1101 0001 0100 1011

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.

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

4251

0011 0010 1010 1101 0001 0100 1011

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 procedureBEFORE which they were declared (huh?)

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

4251

0011 0010 1010 1101 0001 0100 1011

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

OkOk

Ok

Ok

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

4251

0011 0010 1010 1101 0001 0100 1011

In terms of boxes in memory

EVERY variable (global or local) has its separate box in memoryIf you declare 5 global variable and 15 local, there are total of 20 variables in your program

Local GlobalLocalLocalLocalLocalLocalLocalLocalLocalLocalLocalLocalLocal

GlobalGlobalGlobalGlobalGlobal

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

4251

0011 0010 1010 1101 0001 0100 1011

Here is a tricky part

• What if we declare two variable one local one global with THE SAME NAME???

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

4251

0011 0010 1010 1101 0001 0100 1011

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

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

4251

0011 0010 1010 1101 0001 0100 1011

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; VertLinesEND;

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.

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

4251

0011 0010 1010 1101 0001 0100 1011

Programs for this lecture

• Address

• Labels

• Global vs. local