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)

Slides:



Advertisements
Similar presentations
Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.
Advertisements

Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
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.
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.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
Functions:Passing Parameters by Value Programming.
Computer Science 1620 Function Scope & Global Variables.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
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.
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,
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Chapter 6: Functions.
Functions g g Data Flow g Scope local global part 4 part 4.
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.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Learners Support Publications Functions in C++
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
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.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Objective: Students will be able to: Declare and use variables Input integers.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
Variables and memory addresses
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
User-Defined Functions (cont’d) - Reference Parameters.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
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.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Pointers Psst… over there.
Pointers Psst… over there.
Extra.
While Loop Design ENGI 1020 Fall 2018.
Variables and Their scope
Pointers & Functions.
Anatomy of a Function Part 1
Local, Global Variables, and Scope
The Function Prototype
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
CS1201: Programming Language 2
C Parameter Passing.
Presentation transcript:

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) * One of the statements in the function body should have the form: return ; * The value passed back by return should have the same type as the function.

COMP104 Pass by Value / Slide 3 Pass by Value * An important fact to remember about parameter passing by value is that changes to the parameters inside the function body have no effect outside of the function.

COMP104 Pass by Value / Slide 4 Pass by Value: Example 0 * For example, consider the following code: int sum(int a, int b){ a = a + b; return a; } 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 Pass by Value / Slide 5 Pass by Value: Example 0 * The answer: 3, 5, and 8.  Even though the value of parameter a is changed, the corresponding value in variable x does not change. * This is why this is called pass by value. * The value of the original variable is copied to the parameter, but changes to the value of the parameter do not affect the original variable. * In fact, all information in local variables declared within the function will be lost when the function terminates. * The only information saved from a pass by value function is in the return statement.

COMP104 Pass by Value / Slide 6 Pass by Value: Example 1 * An example to show how the function does not affect a variable which is used as a parameter: // Test the effect of a function // on its parameter #include using namespace std; void Increment(int Number) { Number = Number + 1; cout << "The parameter Number is: " << Number << endl; }

COMP104 Pass by Value / Slide 7 Pass by Value: Example 1 void main() { int I = 10; //parameter is a variable Increment(I); cout << "The variable I is: " << I << endl; //parameter is a constant Increment(35); cout << "The variable I is: " << I << endl; //parameter is an expression Increment(I+26); cout << "The variable I is: " << I << endl; }

COMP104 Pass by Value / Slide 8 Pass by Value: Example 1

COMP104 Pass by Value / Slide 9 Pass by Value: Example 2 // 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 PrintSumAve ( double, double ); void main ( ) { double x, y; cout << "Enter two numbers: "; cin >> x >> y; PrintSumAve ( x, y ); }

COMP104 Pass by Value / Slide 10 Pass by Value: Example 2 void PrintSumAve (double no1, double no2) { double sum, average; sum = no1 + no2; average = sum / 2; cout << "The sum is " << sum << endl; cout << "The average is " << average << endl; }

COMP104 Pass by Value / Slide 11 Pass by Value: Example 2  Data areas after call to PrintSumAve() :

COMP104 Pass by Value / Slide 12 Pass by Value: Example 3 //Compute new balance at a certain interest rate //Inputs: A positive balance and // a positive interest rate //Output: The new balance after interest was posted #include using namespace std; double new_balance(double balance, double rate); /* Returns the balance in a bank account after adding interest. For example, if rate is 5.0, then the interest rate is 5% and so new_balance(100, 5.0) returns */

COMP104 Pass by Value / Slide 13 Pass by Value: Example 3 int main(){ double interest, balance; cout << "Enter balance (positive number): "; cin >> balance; if (balance <= 0.0) cout <<"Balance not positive; stopping" << endl; else { cout <<"Enter interest rate (positive number): "; cin >> interest; if (interest <= 0.0) cout << "Interest not positive; stopping" << endl; else cout <<"New balance = $" << new_balance(balance, interest)<< endl; } return 0; }

COMP104 Pass by Value / Slide 14 Pass by Value: Example 3 // New balance is computed as balance + balance * %rate double new_balance(double balance, double rate) { double interest_fraction, interest; interest_fraction = rate / 100; interest = interest_fraction * balance; return (balance + interest); } /* New balance is computed as balance * (1 + %rate) double new_balance(double balance, double rate) { double interest_fraction, updated_balance; interest_fraction = rate / 100; updated_balance = balance * (1 + interest_fraction); return updated_balance; } */

COMP104 Pass by Value / Slide 15 Pass by Value: Example 4 // Input: inches // Output: feet and inches #include using namespace std; // Function prototypes int feet(int); int rinches(int);

COMP104 Pass by Value / Slide 16 Pass by Value: Example 4 void main() { int inches; // Number of inches cout << "Enter number of inches to convert: "; cin >> inches; cout << "Result is " << feet(inches) << " feet " << rinches(inches) << " inches" << endl; } int feet(int inches) { return inches / 12; } int rinches(int inches) { return inches % 12; }

COMP104 Pass by Value / Slide 17 Pass by Value: Example 5 // File i2f.h int feet(int); int rinches(int); // File i2f.cpp // Functions for Converting inches to feet int feet(int inches) { return inches / 12; } int rinches(int inches) { return inches % 12; }

COMP104 Pass by Value / Slide 18 Pass by Value: Example 5 // File main.cpp // Input inches // Output feet and inches #include using namespace std; #include "i2f.h" void main() { int inches; // Number of inches cout << "Enter number of inches to convert: "; cin >> inches; cout << "Result is " << feet(inches) << " feet " << rinches(inches) << " inches" << endl; }