EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Slide 1 Insert your own content. Slide 2 Insert your own content.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
0 - 0.
1 Pointers and Strings Section 5.4, , Lecture 12.
5.9 + = 10 a)3.6 b)4.1 c)5.3 Question 1: Good Answer!! Well Done!! = 10 Question 1:
Addition 1’s to 20.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Test B, 100 Subtraction Facts
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Functions Prototypes, parameter passing, return values, activation frams.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
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.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
Functions Pass by Value Pass by Reference IC 210.
Functions Dilshad M. Shahid New York
Pointers 1 Pointers Pointers  In chapter three we looked at passing parameters to functions using call by value, and using call by reference, using reference.
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.
References Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences National.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
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.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Lecture 12 Pointers and Strings Section 5.4, ,
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
1 Object-Oriented Programming Using C++ A tutorial for pointers.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
1 Lecture 8 Pointers and Strings: Part 2 Section 5.4, ,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1. Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship with arrays and strings 2.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 7 - Pointers Outline 7.1 Introduction
Dissection of AVL tree operations
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointers Psst… over there.
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Pointers & Functions.
Pointers Call-by-Reference CSCI 230
Pointers Kingdom of Saudi Arabia
5.1 Introduction Pointers Powerful, but difficult to master
Function “Inputs and Outputs”
C++ Programming Lecture 17 Pointers – Part I
Pointers & Functions.
CISC181 Introduction to Computer Science Dr
Presentation transcript:

EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST

Calling Functions by Reference  3 ways to pass arguments to function –Pass-by-value –Pass-by-reference with reference arguments –Pass-by-reference with pointer arguments  return can return one value from function  Arguments passed to function using reference arguments –Modify original values of arguments –More than one value “returned”

Calling Functions by Reference  Pass-by-reference with pointer arguments –Simulate pass-by-reference Use pointers and indirection operator –Pass address of argument using & operator –Arrays not passed with & because array name already pointer – * operator used as alias/nickname for variable inside of function

1 2 // Cube a variable using pass-by-value. 3 #include 4 5 using std::cout; 6 using std::endl; 7 8 int cubeByValue( int ); // prototype 9 10 int main() 11 { 12 int number = 5; cout << "The original value of number is " << number; // pass number by value to cubeByValue 17 number = cubeByValue( number ); cout << "\nThe new value of number is " << number << endl; return 0; // indicates successful termination } // end main 24

25 // calculate and return cube of integer argument 26 int cubeByValue( int n ) 27 { 28 return n * n * n; // cube local variable n and return result } // end function cubeByValue The original value of number is 5 The new value of number is 125

1 2 // Cube a variable using pass-by-reference 3 // with a pointer argument. 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 void cubeByReference( int * ); // prototype int main() 12 { 13 int number = 5; cout << "The original value of number is " << number; // pass address of number to cubeByReference 18 cubeByReference( &number ); cout << "\nThe new value of number is " << number << endl; return 0; // indicates successful termination } // end main 25

26 // calculate cube of *nPtr; modifies variable number in main 27 void cubeByReference( int *nPtr ) 28 { 29 *nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr } // end function cubeByReference The original value of number is 5 The new value of number is 125

Relationship Between Pointers and Arrays  Arrays and pointers closely related –Array name like constant pointer –Pointers can do array subscripting operations  Accessing array elements with pointers –Element b[ n ] can be accessed by *( bPtr + n ) Called pointer/offset notation –Addresses &b[ 3 ] same as bPtr + 3 –Array name can be treated as pointer b[ 3 ] same as *( b + 3 ) –Pointers can be subscripted (pointer/subscript notation) bPtr[ 3 ] same as b[ 3 ]

1 2 // Using subscripting and pointer notations with arrays. 3 4 #include 5 6 using std::cout; 7 using std::endl; 8 9 int main() 10 { 11 int b[] = { 10, 20, 30, 40 }; 12 int *bPtr = b; // set bPtr to point to array b // output array b using array subscript notation 15 cout << "Array b printed with:\n" 16 << "Array subscript notation\n"; for ( int i = 0; i < 4; i++ ) 19 cout << "b[" << i << "] = " << b[ i ] << '\n'; // output array b using the array name and 22 // pointer/offset notation 23 cout << "\nPointer/offset notation where " 24 << "the pointer is the array name\n"; 25

26 for ( int offset1 = 0; offset1 < 4; offset1++ ) 27 cout << "*(b + " << offset1 << ") = " 28 << *( b + offset1 ) << '\n'; // output array b using bPtr and array subscript notation 31 cout << "\nPointer subscript notation\n"; for ( int j = 0; j < 4; j++ ) 34 cout << "bPtr[" << j << "] = " << bPtr[ j ] << '\n'; cout << "\nPointer/offset notation\n"; // output array b using bPtr and pointer/offset notation 39 for ( int offset2 = 0; offset2 < 4; offset2++ ) 40 cout << "*(bPtr + " << offset2 << ") = " 41 << *( bPtr + offset2 ) << '\n'; return 0; // indicates successful termination } // end main

Array b printed with: Array subscript notation b[0] = 10 b[1] = 20 b[2] = 30 b[3] = 40 Pointer/offset notation where the pointer is the array name *(b + 0) = 10 *(b + 1) = 20 *(b + 2) = 30 *(b + 3) = 40 Pointer subscript notation bPtr[0] = 10 bPtr[1] = 20 bPtr[2] = 30 bPtr[3] = 40 Pointer/offset notation *(bPtr + 0) = 10 *(bPtr + 1) = 20 *(bPtr + 2) = 30 *(bPtr + 3) = 40