Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables."— Presentation transcript:

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

2 Quiz #3 10 minutes

3 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 this Parameter area P1P2P3 V1V2V3 Local variables area

4 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

5 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.

6 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.

7 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 ); end.

8 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 );

9 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

10 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 iMyGlobalVar 32 Calls SomeProcedure Creates box for iProcParameter Copies value of the iMyGlobalVar into iProcParameter 32

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

12 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!

13 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

14 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.

15 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 );

16 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?

17 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.

18 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.

19 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

20 Homework Sections 6.4 - 6.5 Make sure you are TOTALLY comfortable with them. Optional problems Pages 247-250: Problems 1-4, 11, 12

21 Programs from this lecture Grade histogram Passing parameters BOO 2 By value vs by reference


Download ppt "Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables."

Similar presentations


Ads by Google