CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Functions  Programmer-Defined Functions  Local Variables in Functions  Overloading Function Names  void Functions,  Call-By-Reference Parameters in.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
Learning Objectives  Parameters  Call-by-value  Call-by-reference  Mixed parameter-lists  Overloading and Default Arguments  Examples, Rules  Testing.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Functions. Let’s look at the 2 programs of evaluating: y=2^3+2^5+2^6; #include using namespace std; int main() { int y=0; int partResult=1; for (int i=1;
Parameter Passing Mechanisms Reference Parameters § §
Slide 1 Chapter 4 Parameters and Overloading. Slide 2 Learning Objectives  Parameters  Call-by-value  Call-by-reference  Mixed parameter-lists  Overloading.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first.
Chapter 9 Functions Dept of Computer Engineering Khon Kaen University.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
1 FUNCTIONS - II Chapter 9 and Today we will continue with functions covering…. Passing by Reference  Scope Sorting variables Function Structure.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
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.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
C++ Programming Lecture 12 Functions – Part IV
User-Defined Functions (cont’d) - Reference Parameters.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 8 September 24, 2009.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Functions CMSC 202. Topics Covered Function review More on variable scope Call-by-reference parameters Call-by-value parameters Function overloading Default.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
A Lecture for the c++ Course
Pointers and Pointer-Based Strings
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Reference parameters (6.2.3)
Extra.
6.12 Default Arguments.
6 Chapter Functions.
Parameters and Overloading
Pointers and Pointer-Based Strings
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CMSC 202 Lesson 6 Functions II.
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Programming Fundamental
Presentation transcript:

CSIS 113A Lecture 8 Parameters

 Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address of’ actual argument is passed

Call-by-Value Parameters  Copy of actual argument passed  Considered ‘local variable’ inside function  If modified, only ‘local copy’ changes  Function has no access to ‘actual argument’ from caller  This is the default method  Used in all examples thus far

Example #include using namespace std; void foo(int z); int main() { int x = 3; foo(x); } void foo(int z) { cout << z; }

Example Depicted

Call-by-Value Pitfall  Common Mistake:  Declaring parameter ‘again’ inside function: double fee(int hoursWorked, int minutesWorked) { int quarterHours;// local variable int minutesWorked// NO! }  Compiler error results  “Redefinition error…”  Value arguments ARE like ‘local variables’  But function gets them ‘automatically’

Call-By-Reference Parameters  Used to provide access to caller’s actual argument  Caller’s data can be modified by called function!  Typically used for input function  To retrieve data for caller  Data is then ‘given’ to caller  Specified by ampersand, &, after type in formal parameter list

By Reference

By Reference II

Example #include #include using namespace std; void swap(int &x, int &y); int main() { int a = 2; int b = 3; cout << "Before Swap " << a << " " << b << endl; swap(a, b); cout << "After Swap " << a << " " << b << endl; return 0; } void swap(int &x, int &y) { int tmp; tmp = x; x = y; y = tmp; }

Constant Reference Parameters  Reference arguments inherently ‘dangerous’  Caller’s data can be changed  Often this is desired, sometimes not  To ‘protect’ data, & still pass by reference:  Use const keyword  void sendConstRef(const int &par1, const int &par2);  Makes arguments ‘read-only’ by function  No changes allowed inside function body

Parameters and Arguments  Confusing terms, often used interchangeably  True meanings:  Formal parameters  In function declaration and function definition  Arguments  Used to ‘fill-in’ a formal parameter  In function call (argument list)  Call-by-value & Call-by-reference  Simply the ‘mechanism’ used in plug-in process

Mixed Parameter Lists  Can combine passing mechanisms  Parameter lists can include pass-by-value and pass-by-reference parameters  Order of arguments in list is critical: void mixedCall(int & par1, int par2, double & par3);  Function call: mixedCall(arg1, arg2, arg3);  arg1 must be integer type, is passed by reference  arg2 must be integer type, is passed by value  arg3 must be double type, is passed by reference

Choosing Formal Parameter Names  Same rule as naming any identifier:  Meaningful names!  Functions as ‘self-contained modules’  Designed separately from rest of program  Assigned to teams of programmers  All must ‘understand’ proper function use  OK if formal parameter names are same as argument names  Choose function names with same rules

Default Arguments allow you to set a value that will serve as a defualt value if you don't supply a parameter

Example #include using namespace std; int calcArea(int length, int width = 2); int main() { int a1, a2; a1 = calcArea(2, 5); a2 = calcArea(3); } int calcArea(int length, int width) { return length*width; }

Default Parameter Info Always defined in the function prototype. – compiler needs to see the definition of what you are doing before a call to the function can be made. If you are going to write functions that have some of the arguments defaulted and others not, –you have to make sure that the default arguments always appear last. You could not do the following: int calcArea(int length = 3, int width);