Functions ----------- Pass by Value Pass by Reference IC 210.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
1 Pointers and Strings Section 5.4, , Lecture 12.
CISC Data Structures Ben Perry University of Delaware Summer 2011.
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)
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Functions Prototypes, parameter passing, return values, activation frams.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Functions:Passing Parameters by Value Programming.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
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.
#include using namespace std; void main() { cout
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 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
Functions Modules in C++ are called functions and classes
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.
 2003 Prentice Hall, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate pass-by-reference –Close relationship.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science Functions.
Passing Data - by Reference Syntax && double Pythagorus(double &, double &); Pythagorus(height, base); & & double Pythagorus(double& a, double& b) function.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
1 CS161 Introduction to Computer Science Topic #10.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Learners Support Publications Functions in C++
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
User-Defined Functions (cont’d) - Reference Parameters.
CSci 162 Lecture 6 Martin van Bommel. Functions on Structures Rather than pass a copy of structure to a function, or return a copy back as the function.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Functions and an Introduction to Recursion.  Write a program for learn C++ subfunction.  Exercise: ◦ Please implement the following functions:  double.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
CS 1430: Programming in C++.
Scope, Parameter Passing, Storage Specifiers
Anatomy of a Function Part 2
Heterogeneous aggregate datatypes
User-defined Functions
Name: Rubaisha Rajpoot
Anatomy of a Function Part 1
Pointers Call-by-Reference CSCI 230
Lec 14 Oct 23, 02.
5.1 Introduction Pointers Powerful, but difficult to master
Function “Inputs and Outputs”
The Function Prototype
Pointers and dynamic objects
Anatomy of a Function Part 1
Unit-1 Introduction to Java
Functions Reasons Concepts Passing arguments to a function
C Parameter Passing.
Presentation transcript:

Functions Pass by Value Pass by Reference IC 210

Arguments vs. Parameters Arguments: function call actual values passed to function Parameters: Placeholder in function heading waiting for arguments answer = cube (x); double cube (double inputVal) Argument (function call) parameter (function heading)

What is the output? int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; } ?? 5 Call by Value

X results Call by value: “Sending a copy” 5 DoubleIt ? int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }

X results Call by value: “Sending a copy” 5 ? ? DoubleIt int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }

X results Call by value: “Sending a copy” 5 ? ? DoubleIt int DoubleIt (int); int main ( ) { int x = 5; results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; } y ?

X results y Call by value: “Sending a copy” 5 ? ? ? X’s value is copied into Y DoubleIt int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }

X results y Call by value: “Sending a copy” 5 ? ? 5 DoubleIt int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }

X results y Call by value: “Sending a copy” 5 5 ? 10 DoubleIt ? int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }

X results y Call by value: “Sending a copy” 10 DoubleIt 10 int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; } 5 ?

X results y Call by value: “Sending a copy” 5 ? DoubleIt 10 int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }

X results Call by value: “Sending a copy” 5 10 DoubleIt 10 int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }

X results Call by value: “Sending a copy” 5 10 DoubleIt ? int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }

X results Call by value: “Sending a copy” 5 DoubleIt ? 10 int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }

int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) // Function Definition { int z = y; y = 2 * y; return y + z; } What is the output? ?? 14,21 Call by Reference

int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) // Function Definition { int z = y; y = 2 * y; return y + z; } Call by reference: “Getting the original” TripleIt x 7

int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) // Function Definition { int z = y; y = 2 * y; return y + z; } results Call by reference: “Getting the original” TripleIt ? x 7

results ? ? TripleIt Call by reference: “Getting the original” int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } x 7

int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } results ? ? TripleIt Call by reference: “Getting the original” x 7

int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } results y, x ? ? X’s address is referenced TripleIt Call by reference: “Getting the original” 7

int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } results y, x ? TripleIt ? Call by reference: “Getting the original” z 7 7

int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } results y, x ? TripleIt ? Call by reference: “Getting the original” z 7 714

int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } results y, x ? TripleIt 14 ? Call by reference: “Getting the original” z 7 21

results y, x ? TripleIt Call by reference: “Getting the original” int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } z 7 _

results 21 TripleIt 21 Call by reference: “Getting the original” int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } x 14

results 21 TripleIt ? Call by reference: “Getting the original” int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } x 14