lecture 16 pass by value vs. pass by reference (still) appearances are often deceiving aesop, fables

21
4 2 5 1 0011 0010 1010 1101 0001 0100 1011 Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

Upload: eleanore-nicholson

Post on 16-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

42510011 0010 1010 1101 0001 0100 1011

Lecture 16

Pass by value vs. pass by reference

(Still)

Appearances are often deceiving

Aesop, Fables

Page 2: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Quiz #3

• 10 minutes

Page 3: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Last lecture review• In order to make procedures more powerful we

introduced a concept of parameter passing

• When procedure is invoked several parameters can be passed into it

• We can pass a constant or a variable

• Parameters are just like variables

• Think of procedure’s memory like thisParameter area

P1 P2 P3

V1 V2 V3 Local variables area

Page 4: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Optional program from last 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

Page 5: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

I am still confused...

• You know I am still sort of shaky on the concept of parameters. I understand why procedures are useful and somewhat see why you would pass parameters to them, but it doesn’t click yet.

• Well, this is reasonable, we just learned it last time. Okay, I think I know what we need to do before covering the last (and probably the hardest) topic about procedures. Lets review the concepts we looked at.

Page 6: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Procedure related concepts

1) Global vs. local variables. Global variables are declared outside of any procedure. Local are declared inside some procedure and can only be accessed within this procedure

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.

Page 7: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Procedure related concepts

2) Name conflict.

Global variable which has the same name as local variable can not be accessed. It is shadowed by the local variable. We just have to be aware of this. We should never purposely name global and local variable with the same name.

Program ShadowingDemo;

var iPoorMe : integer;

procedure proc1;

var iPoorMe : integer;

begin

iPoorMe := 20;

writeln( iPoorMe );

end;

begin

iPoorMe := 10;

writeln(iPoorMe );

writeln(iPoorMe );

end.

Page 8: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Procedure related concepts

3) Parameters

To make procedures more flexible and more powerful we allowed programmers to pass parameters to procedure. Parameters can be constants or variables.

And here is what I am confused about: How come ‘x’ from main program becomes ‘iLineLength’ ???

procedure FilledLine( iLineLength : integer )

var iCount : integer;

begin

for iCount := 1 to iLineLength do

begin

write( ‘*’ );

end;

writeln;

end;

var x : integer;

{ Main program }

begin

FilledLine( 10 );

x := 23;

FilledLine( x );

Page 9: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Parameter passing

• Excellent question. This is indeed confusing. Here is an idea. Procedure is written in general terms. It doesn’t really care what you pass to it. Consider the following pseudo-code:

procedure StoreInCloset( String Item )

begin

end

{ Main program }

var sItem1, sItem2 : String[ 30 ];

begin

sItem1 = ‘Shirt’;

sItem2 = ‘Tie’;

StoreInCloset( sItem1 );

StoreInCloset( sItem2 );

end

Page 10: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Parameter passing

• Now, as I said before, what happens is this:

Procedure SomeProcedure( int iProcParameter )begin…end

{ Main program }var iMyGlobalVar : integer;begin iMyGlobalVar := 32; SomeProcedure( iMyGlobalVar );end

Var iMyGlobalVar : integer;

iMyGlobalVar := 32;

SomeProcedure( iMyGlobalVar );

Allocates box iMyGlobalVar

Places 32 in the box iMyGlobalVar32

Calls SomeProcedureCreates box for iProcParameterCopies value of the iMyGlobalVar into iProcParameter

iProcParameter32

Page 11: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Parameter passing

• Okay, I think I got it this time. Just to make sure let me write a practice program.

Page 12: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Parameter passing

• Finally, I think I just got a cool idea. I can modify FilledLine and ShallowLine so that not only they produce a variable length line, but also accept character as a parameter!

Page 13: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Better FilledLine

procedure FilledLine( iLineLength : integer; cPen : char )

var iCount : integer;

begin

for iCount := 1 to iLineLength do

begin

write( cPen );

end;

writeln;

end;

Now this is really powerful

Page 14: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Parameter passing

• Okay, I think that you are now ready to learn yet another thing which makes procedures much more powerful yet.

• Lets start of by recalling a program, which we wrote a while ago - grade average. Remember we had a nested for-loop and prompted user for grade info, while adding and averaging grades. Suppose that somehow we could say to procedure: Compute the average grade of a student and then tell me what it is. This would be very useful. But so far, we didn’t have any way of getting information out of procedure, we just made perform some computation.

Page 15: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Parameter passing

• Our motivations are really based on real life. Think about it this way. Our main program is a conductor, it governs the flow of computation by calling appropriate procedures. In our brain, procedures get invoked all the time, but the brain is more powerful, because it allows the data to be passed to, from and between procedures. We want to be able to pass data into procedure and get something out as well. Now, compare the following:

Procedure Test( iOneHappyInteger : integer );Procedure Test( var iOneHappyInteger : integer );

Page 16: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Passing parameters

• Okay, you are saying that we will be able to change the value of parameter, which is passed in? Hmmm… Now, from what I understood before, when you call a procedure and you pass a variable into it as a parameter, the procedure makes a copy?! So how can it be that the value of the original one would be different?

Page 17: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Parameter passing

• The reason for this is because var says, don’t make a copy! It says, let me access the variable passed into the procedure directly! This is yet another concept which comes with procedures.

Page 18: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Pass by value vs pass by reference

Procedure Test( iOneHappyInteger : integer );

Without preceding keyword var, a parameter is considered to be passed by value. This means that when procedure is called, a copy of the passed variable is made to be used in this procedure. Should the value of this copy change during the execution of the procedure, the original value WILL NOT be changed.

Procedure Test( iOneHappyInteger : integer );

With preceding keyword var, a parameter is considered to be passed by reference. This means that when procedure is called, a the reference to the actual variable will be used in this procedure. Should the value of this parameter change during the execution of the procedure, the original value WILL be changed.

Page 19: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

By value vs by reference

Procedure myProcedure( iOneInteger : integer );

begin

iOneInteger := 20;

end

var iMyVar : integer;

begin

iMyVar := 33;

myProcedure( iMyVar );

end

Procedure myProcedure( VAR iOneInteger : integer );

begin

iOneInteger := 20;

end

var iMyVar : integer;

begin

iMyVar := 33;

myProcedure( iMyVar );

end

WILL change the iMyVar

Will NOT change the iMyVar

Page 20: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Homework

• Sections 6.4 - 6.5

• Make sure you are TOTALLY comfortable with them.

• Optional problems Pages 247-250:

• Problems 1-4, 11, 12

Page 21: Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables

4251

0011 0010 1010 1101 0001 0100 1011

Programs from this lecture

• Grade histogram

• Passing parameters

• BOO 2

• By value vs by reference