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

Slides:



Advertisements
Similar presentations
C Programming lecture 2 Beautiful programs Greek algorithms
Advertisements

An introduction to pointers in c
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Introduction to computers & programming Part Deux Abandon all hope, ye who enter here Dante, The Divine Comedy.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
VBA Modules, Functions, Variables, and Constants
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 201 Functions Debzani Deb.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 7: Methods.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
The switch Statement, DecimalFormat, and Introduction to Looping
Hello AP Computer Science!. What are some of the things that you have used computers for?
Using the API. Learning Objectives By the end of this lecture, you should be able to: – Identify what is meant by an ‘API’ – Know how to look up functions.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Java Data Types Data types, variable declaration, and initialization.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CPS120: Introduction to Computer Science Decision Making in Programs.
Object-Oriented Programming in C++
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3.
Methods We write methods in our programs for many reasons:
JavaScript, Fourth Edition
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Arrays: Part Opening Discussion zWhat did we talk about last class? zWhat do you think the picture of memory looks like when we declare.
Intermediate 2 Computing Unit 2 - Software Development.
Lecture 15 Variable passing Appearances are often deceiving Aesop, Fables.
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Controlling Program Flow with Decision Structures.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Lecture 11 Instructor: Craig Duckett Instance Variables.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
User-Written Functions
INTRODUCTION TO PROBLEM SOLVING
Chapter 7: User-Defined Functions II
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
CSC 253 Lecture 8.
Functions Inputs Output
Lesson 2: Building Blocks of Programming
Chapter 10 Programming Fundamentals with JavaScript
Lecture 11 C Parameters Richard Gesick.
CSC 253 Lecture 8.
One-Dimensional Array Introduction Lesson xx
Chapter 4 void Functions
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Procedures Brent M. Dingle Texas A&M University
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Arrays.
Miscellaneous Topics I
Presentation transcript:

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

Quiz #3 10 minutes

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

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

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.

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.

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.

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

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

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

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

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!

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

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.

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

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?

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.

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.

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

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

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