Mark Dixon, School of Computing SOFT 120Page 1 5. Passing Parameters by Reference.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Substitution.
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Chapter 17 vector and Free Store Bjarne Stroustrup
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Introduction to C Programming
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Chapter 7: Arrays In this chapter, you will learn about
CS1104 – Computer Organization
LIST PROCESSING.
ABC Technology Project
Hash Tables.
CS2100 Computer Organisation
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
CISC Data Structures Ben Perry University of Delaware Summer 2011.
Review Pseudo Code Basic elements of Pseudo code
1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
C Language.
25 seconds left…...
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Chapter Three Arithmetic Expressions and Assignment Statements
We will resume in: 25 Minutes.
Pointers and Arrays Chapter 12
Data Structures Using C++ 2E
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
Code Generation.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Mark Dixon, School of Computing SOFT 120Page 1 4. User Defined Functions (part 2)
Variables and memory addresses
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers and Pointer-Based Strings
Using local variable without initialization is an error.
Function “Inputs and Outputs”
Pointers and Pointer-Based Strings
Presentation transcript:

Mark Dixon, School of Computing SOFT 120Page 1 5. Passing Parameters by Reference

Mark Dixon, School of Computing SOFT 120Page 2 Variables and Memory Addresses The computer keeps trace of where variables are stored in memory by using memory addresses. Every byte (position) in memory has a memory address: In the above example the variable identified by the name x is stored at location (this is the address of the first byte of data allocated to the variable x) 23xInteger Identifier Value Type Memory

Mark Dixon, School of Computing SOFT 120Page 3 Parameter Passing Methods There are 2 ways to pass parameters to functions and procedures: –Passing by Value: a literal value is passed from the call to the definition (you have already used this) procedure p1(x: integer); begin … end; –Passing by Reference: a variable’s memory address (a reference to the variables position in memory) is passed from the call to the definition procedure p2(var y: integer); begin … end;

Mark Dixon, School of Computing SOFT 120Page 4 Why pass by reference? It allows the value of the passed variable to be changed –i.e. it allows functions and procedures to change the value of things passed to them Normally parameters are for input data – only functions can output data via the return value Pass by reference allows data to be input and output via parameters

Mark Dixon, School of Computing SOFT 120Page 5 Example: Change the Value procedure P1(x: integer); begin x := x * 2; end; procedure P2(var x: integer); begin x := x * 2; end; var a, b: integer; a := 11; b := 12; P1(a);// What is the value of a? P2(b);// What is the value of b?

Mark Dixon, School of Computing SOFT 120Page 6 What can be passed Pass by value – both literals and variables can be passed (variables are substituted by their value) p1(y); // This is fine. p1(21); // This is fine. Pass by reference – only variables can be passed (in fact the variables memory address is passed) literals cannot be passed – they have no memory address p2(y); // This is fine. p2(21); // This generates an error. 

Mark Dixon, School of Computing SOFT 120Page 7 Example: Pass by Ref vs. Function procedure P2(var x: integer); begin x := x * 2; end; function F2(x: integer): integer; begin Result := x * 2; end; var b: integer; b := 4; P2(b);// What is the value of b? b := 4; b := F2(b);// What is the value of b? P1 var x: integer F1 x: integerinteger

Mark Dixon, School of Computing SOFT 120Page 8 Pass by Ref vs. Function a procedure that changes the value of a single parameter is equivalent to a function, –the procedure P2: P2(b); was equivalent to: –the function F2: b := F2(b); However, –F2 is far more explicit, –P2 is a bit cryptic: not obvious that value of b changes this makes code difficult to read, which can lead to errors

Mark Dixon, School of Computing SOFT 120Page 9 Example: Total var Nums: array[1..5] of integer; var tot: integer; function Total(): integer; var tmpTot: integer; var i: integer; begin tmpTot := 0; for i := 1 to 5 do tmpTot := tmpTot + Nums[i]; Result := tmpTot; end; Nums[1] := 23; Nums[2] := 17; Nums[3] := 28; Nums[4] := 12; Nums[5] := 25; tot := Total();// What is the value of tot?

Mark Dixon, School of Computing SOFT 120Page 10 Example: Average var ave: double; function Average(): double; var tmpTot: integer; var i: integer; begin tmpTot := 0; for i := 1 to 5 do tmpTot := tmpTot + Nums[i]; Result := tmpTot / 5; end; ave := Average();// What is the value of ave?

Mark Dixon, School of Computing SOFT 120Page 11 Two results? Total and Average functions share a lot of code Useful to combine them Problem: –a function can only have 1 output –This: is not possible (in Delphi anyway) double (average) integer (total) TotAve

Mark Dixon, School of Computing SOFT 120Page 12 Example: Total and Average procedure TotAve(var T: integer; var A: double); var i: integer; begin T := 0; for i := 1 to 5 do begin T := T + Nums[i]; end; A := T / 5; end; tot := 12; ave := 15; TotAve(tot, ave); // What is the value of ave and tot? TotAve var T: integer var A: double