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.

Slides:



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

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.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Programming Functions: Passing Parameters by Reference.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
1 Lecture 10 Chapter 7 Functions Dale/Weems/Headington.
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.
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
If Statements & Relational Operators Programming.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
Problem Solving and Program Design. COMP104 Problem Solving / Slide 2 Our First Program // a simple program #include using namespace std; int main() {
Writing and Testing Programs Drivers and Stubs Supplement to text.
Passing Arrays to Functions. COMP104 Lecture 16 / Slide 2 Array Element Pass by Value * Individual array elements can be passed by value or by reference.
Functions:Passing Parameters by Value Programming.
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.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double.
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
Function Part II: Some ‘advanced’ concepts on functions.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Chapter 6: Functions.
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.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
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.
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.
Functions—Part I. Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
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 5 Functions for All Subtasks Goals: To explore the use of void functions To explore the use of void functions To distinguish between call-by-reference.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Chapter 5 Function Basics
A Lecture for the c++ Course
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Functions.
Functions.
CS150 Introduction to Computer Science 1
Counting Loops.
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Pass by Reference.
CS149D Elements of Computer Science
Statements and flow control
CS150 Introduction to Computer Science 1
Pointers and dynamic objects
Pointers & Functions.
TOPIC: FUNCTION OVERLOADING
CS150 Introduction to Computer Science 1
Presentation transcript:

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 pass by reference. l Reference (address) of parameter is passed to the function, instead of its value. l If the function changes the parameter value, the changes will be reflected in the program calling it. l How to pass parameters by reference: &,..., &

COMP104 Lecture 19 / Slide 3 Pass by Value: Example 0 l For example, consider the following code: int sum(int x, int y){ x = x + y; return x; } void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); } What is the value of x, y, and z at the end of the main() program?

COMP104 Lecture 19 / Slide 4 void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); } x y 35 x y 85 int sum(int x, int y){ x = x + y; return x; } Pass by Value: Example 0 x 3 y 5 z - x 3 y 5 z 8

COMP104 Lecture 19 / Slide 5 Pass by Reference: Example 0 l For example, consider the following code: int sum(int &x, int &y){ x = x + y; return x; } void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); } What is the value of x, y, and z at the end of the main() program?

COMP104 Lecture 19 / Slide 6 x 3 y 5 z - void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); } x y main::x main::y int sum(int &x, int &y){ x = x + y; return x; } Pass by Reference: Example 0 x 3 y 5 z - 88

COMP104 Lecture 19 / Slide 7 Pass by Reference: Example 1 l To show how the function affects a variable which is used as a parameter: #include using namespace std; void Increment(int& Number){ Number = Number + 1; cout << "The parameter Number: " << Number << endl; } void main(){ int I = 10; Increment(I); // parameter is a variable cout << "The variable I is: " << I << endl; }

COMP104 Lecture 19 / Slide 8 Pass by Reference: Example 2 l It is possible to use both pass by reference and pass by value parameters in the same function. // Print the sum and average of two numbers // Input: two numbers x & y // Output: sum - the sum of x & y // average - the average of x & y #include using namespace std; void SumAve (double, double, double&, double&);

COMP104 Lecture 19 / Slide 9 Pass by Reference: Example 2 void main ( ) { double x, y, sum, mean; cout << "Enter two numbers: "; cin >> x >> y; SumAve (x, y, sum, mean); cout << "The sum is " << sum << endl; cout << "The average is " << mean << endl; } void SumAve(double no1, double no2, double& sum, double& average) { sum = no1 + no2; average = sum / 2; }

COMP104 Lecture 19 / Slide 10 Pass by Reference: Example 2 l Data areas after call to SumAve:

COMP104 Lecture 19 / Slide 11 Pass by Reference: Example 3 // Compare and sort three integers #include using namespace std; void swap (int&, int&); void main ( ) { int first, second, third; // input integers // Read in first, second and third. cout << "Enter three integers: "; cin >> first >> second >> third; if (first > second) swap (first, second); if (second > third) swap (second, third); if (first > second) swap (first, second); cout << "The sorted integers are " << first << ", " << second << ", " << third << endl; }

COMP104 Lecture 19 / Slide 12 Pass by Reference: Example 3 // Function for swapping two integers void swap (int& x, int& y) { int temp; temp = x; x = y; y = temp; }

COMP104 Lecture 19 / Slide 13 Pass by Reference: Example 4 // Pass-by-reference verses pass-by-value example #include using namespace std; void One (int a, int b, int& c) { int d; a = 10; b = 11; c = 12; d = 13; cout << "The values of a, b, c, and d in One: "; cout << a << " " << b << " " << c << " " << d << endl; } void Two (int a, int b, int& d) { int c = 0; cout << "The values of a, b, c, and d in Two: "; cout << a << " " << b << " " << c << " " << d << endl; }

COMP104 Lecture 19 / Slide 14 Pass by Reference: Example 4 void main () { int a = 1, b = 2, c = 3, d = 4; One(a, b, c); cout << "The values of a, b, c, and d after One: "; cout << a << " " << b << " " << c << " " << d << endl; Two(a, b, d); cout << "The values of a, b, c, and d after Two: "; cout << a << " " << b << " " << c << " " << d << endl; }

COMP104 Lecture 19 / Slide 15 Testing and Debugging Functions l One major advantage of functions is that they can be designed, coded and tested separately from the rest of the program. l Use a "driver" program to test a function with several inputs: void main( ) { int i; for (i = 1; i <= 13; i++){ printcard(i); cout << " " ; }

COMP104 Lecture 19 / Slide 16 Testing and Debugging Functions l If a yet-to-be written function is needed in testing a program, replace it with a "stub" for testing. l A stub has the same interface as the original function, but not the full implementation. Oftentimes, a stub contains just a simple return or cout command. void printcard(int i) { cout << i; }