Pointers & Functions.

Slides:



Advertisements
Similar presentations
1 Pointers and Strings Section 5.4, , Lecture 12.
Advertisements

EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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)

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
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.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Programming Functions: Passing Parameters by Reference.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
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.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
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.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Chapter 8 Exercises.
Pointer* Review Jason Stredwick.
#define #include<iostream> using namespace std; #define GO
Pointers & Arrays.
Command Line Arguments
Chapter 5 Function Basics
School of EECS, Peking University
Pointers and Pointer-Based Strings
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Lecture 8 – 9 Arrays with in a class
Pointer Data Type and Pointer Variables
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Pointer Basics Psst… over there.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Extra.
Random Number Generation
Chapter 5 Function Basics
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS150 Introduction to Computer Science 1
Starting Out with C++: From Control Structures through Objects
Code::Block vs Visual C++
Pass by Reference.
CS150 Introduction to Computer Science 1
Dynamic Memory A whole heap of fun….
CS1201: Programming Language 2
Local, Global Variables, and Scope
Pointers and Pointer-Based Strings
CS150 Introduction to Computer Science 1
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Pointers & Arrays.
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers and dynamic objects
Pointers & Functions.
The Stack.
Pointer Data Type and Pointer Variables
Pointer Basics Psst… over there.
Pointer Data Type and Pointer Variables
CS150 Introduction to Computer Science 1
Methods and Data Passing
Presentation transcript:

Pointers & Functions

Pointers and Functions Functions can take pointers: C equivalent to pass by ref #include <iostream> using namespace std; //C++ style void doubleVariable(int& x) { x = x * 2; } //C style void doubleVariableCStyle(int* x) { *x = (*x) * 2; } int main() { int num = 10; cout << num << endl; doubleVariable(num); cout << num << endl; doubleVariableCStyle(&num); cout << num <<#include <iostream> using namespace std; //C++ style void doubleVariable(int& x) { x = x * 2; } //C style void doubleVariableCStyle(int* x) { *x = (*x) * 2; int main() { int num = 10; cout << num << endl; doubleVariable(num); doubleVariableCStyle(&num); return 0; endl; return 0; }

Passing Pointers Pointers used like reference params: p1 p2 x 5 y 4

Passing Pointers Pointers used like reference params: temp p1 p2 x 5 y 4

Passing Pointers Pointers used like reference params: temp 5 p1 p2 x 5 y 4

Passing Pointers Pointers used like reference params: temp 5 p1 p2 x 4 y 4

Passing Pointers Pointers used like reference params: temp 5 p1 p2 x 4 y 5

Passing Pointers Pointers used like reference params: p1 p2 x 4 y 5

Passing Pointers Directly passing pointers p1 p2 x 5 pt1 0x004 pt2

Passing Pointers Directly passing pointers – addresses copied Can change x/y or p1/p2 Can't change pt1 / pt2 p1 0x004 p2 0x008 x 5 pt1 0x004 pt2 0x008 y 4

Passing Pointers Passing references to pointers p1 p2 x 5 pt1 0x004 y 4

Passing Pointers References to pointers temp References to pointers Can change the pointers themselves p1 p2 x 5 pt1 0x004 pt2 0x008 y 4

Passing Pointers References to pointers temp 0x004 References to pointers Can change the pointers themselves p1 p2 x 5 pt1 0x004 pt2 0x008 y 4

Passing Pointers References to pointers temp 0x004 References to pointers Can change the pointers themselves p1 p2 x 5 pt1 0x008 pt2 0x008 y 4

Passing Pointers References to pointers temp 0x004 References to pointers Can change the pointers themselves p1 p2 x 5 pt1 0x008 pt2 0x004 y 4

Passing Pointers References to pointers Can change the pointers themselves p1 p2 x 5 pt1 0x008 pt2 0x004 y 4

Return Pointers Can return a pointer from a function:

Returning Pointers Can return pointer – better not be to local variable

Returning Pointers Can return pointer – better not be to local variable x 10 pt

Returning Pointers Can return pointer – better not be to local variable x 10 pt 0x110 p 0x110

Returning Pointers Can return pointer – better not be to local variable p 0x110