1 CS 105 Lecture 12 Pass- By-Reference, 2-D Arrays Version: Wed, Apr 20, 2011, 6:25 pm.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Programming Functions: Passing Parameters by Reference.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
©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.
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
1 CS 105 Lecture 13Vectors version of Wed, Apr 27, 2011, 6:20 pm.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
Upcoming Events Project Check Due Next Week in Lab –Phase I –main(), menu() Functions and Reading in File –Stub Functions Last Lectures Next Week –Project.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
1 CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm.
Announcements Final Exam:. Review Passing Arrays as Parameters Pass Array Name into Function int intArray[100]; func(intArray); Accept into Function as.
 CS105 C++ Lecture 2 Arrays. Parameter passing  2 types  Pass-by-value  Pass-by-reference 2.
Chapter 7 Functions.
Programming Functions: Passing Parameters by Reference.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
COMP104B Introduction to Comp Sci 2 Introduction and Revision 1.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 6 Functions.
POINTERS.
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Variables with Memory Diagram
Chapter 5 Function Basics
Pointers & Functions.
Given the code to the left:
Let’s all Repeat Together
Arrays an array of 5 ints is filled with 3,2,4,1,7
Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without.
Pointers and dynamic objects
Pointers & Functions.
Reading from and Writing to Files Part 2
Announcements Exam 2 Lecture Grades Posted on blackboard
Presentation transcript:

1 CS 105 Lecture 12 Pass- By-Reference, 2-D Arrays Version: Wed, Apr 20, 2011, 6:25 pm

2 Exam 2 Lecture Sort% Exam 2 (Lecture) 17 Scores Avg 50.2/67 (or 75/100)Std dev 10.5

3 Exam 2 Questions NbrStudy Question 1 Similar to Ch 5 ex 12 (exam used / instead of %) 2Ch 3 ex 3 3Ch 5 ex 16 4Ch 3 ex 5 5Similar to Ch 6 ex 2 6Ch 10 ex 2

4 Project Posted on Due date ← Discuss! Fri Apr 29 (2400 hrs)? Or Wed May 4 at noon? Hand in to assignment folder in Blackboard Don't send to TA, don't to me. Include.cpp (source code), Study the skeleton and think before coding. No working together. May ask you to come in to discuss program.

5 Pass by Reference Pass-By-Reference means passing a location of a variable into a function Modifying variable in function will modify value in original variable In C++, function asks for pass by reference by using the ampersand (&) before parameter name E.g. int func(int &num)

6 Function Pass by Reference #include using namespace std; void func1(int &num); void func2(int num); main() { int x = 10; func1(x); cout << x << endl;// Prints 0, not 10 // Can't call: func1(10) x = 10; func2(x); cout << x << endl;// Prints 10, not 0 } void func1(int &num) { num = 0; cout << num << endl;// Prints 0 } void func2(int num) { num = 0; cout << num << endl;// Prints 0 }

7 Passing Arrays as Parameters Caller has an array E.g., int intArray[100]; Caller specifies name of array and length separately E.g., func(intArray, 100); Callee header specifies an array with size passed as separate parameter: E.g., int func(int array[], int len) Changes to array by callee does update caller's array.

8 Passing Arrays Example void setZero(int passedArray[], int len); main() { int intArray[5]; func(intArray, 5); cout << intArray[0]; } // setZero(passedArray, len) sets // passedArray[0], passedArray[1],..., // passedArray[len-1] all to 0. // void setZero(int passedArray[], int len) { int i; for (i = 0; i < len ; i++) passedArray[i] = 0; }

9 Two-Dimensional Arrays char letters[10][20]; int values[100][50]; letters[0][0] = 'a'; letters[0][1] = 'b'; letters[9][19] = 'x'; values[0][0] = 99; values[1][5] = 135; values[99][49] = 0;