Lecture 10 Oct 7, 02. Pass by value and reference ► Calling a function and passing the value of a variable as an argument is called pass by value. The.

Slides:



Advertisements
Similar presentations
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)
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
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.
Short C++ Review CS 302 – Data Structures. Call by value/reference.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
Lecture 11 Oct 14, 02. Recursion ► The programs we have discussed are generally structured as functions that call one another in a disciplined, hierarchical.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Functions Pass by Value Pass by Reference IC 210.
Lecture 9 Oct 2, 02. Functions ► Function is a self contained block of program that performs a coherent task of some kind. ► Eg: Consider an automobile,
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.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Programming Functions: Passing Parameters by Reference.
Functions g g Data Flow g Scope local global part 4 part 4.
Functions Structured Programming 256 Chapter 6 Functions g prototypes g arguments g overloading g return values part I.
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.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
Pointers CSE 5100 Data Structures and Algorithms.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
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.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
User-Defined Functions (cont’d) - Reference Parameters.
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
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.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
C++ Review Data Structures.
Functions prototypes arguments overloading return values part I.
CS1201: Programming Language 2
Alternate Version of STARTING OUT WITH C++ 4th Edition
Value returning Functions
CS150 Introduction to Computer Science 1
Pointers & Functions.
The Run-Time Stack and Reference Parameters
Lecture 12 Oct 16, 02.
Lec 14 Oct 23, 02.
Parameter Passing in Java
CS150 Introduction to Computer Science 1
CS1201: Programming Language 2
CSC1201: Programming Language 2
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
CS1201: Programming Language 2
Intro to Programming Week # 8 Functions II Lecture # 13
CS150 Introduction to Computer Science 1
Presentation transcript:

Lecture 10 Oct 7, 02

Pass by value and reference ► Calling a function and passing the value of a variable as an argument is called pass by value. The examples we have till now explained are for pass by value. ► Calling a function and passing the address of a variable as an argument that will be accepted as a reference parameter is called pass by reference

Pass by value ( Lec 9 eg ) ► #include ► #include int calsum(int, int, int) // function prototype declaration int calsum(int, int, int) // function prototype declaration int main() int main() { int a,b,c,sum; int a,b,c,sum; cout<<“Enter any three numbers”<<endll cout<<“Enter any three numbers”<<endll cin>>a>>b>>c; cin>>a>>b>>c; sum=calsum(a,b,c); // function call done here sum=calsum(a,b,c); // function call done here return 0; return 0; } // The function calsum // The function calsum int calsum(int x, int y, int z) // function header int calsum(int x, int y, int z) // function header { int d; int d; d = x+y+z; d = x+y+z; return(d); return(d); }

Pass by reference (example 1) ► #include ► #include void newval(float&, float&); // function prototype decaration void newval(float&, float&); // function prototype decaration int main() int main() { float firstnum, secnum; float firstnum, secnum; cout<<“Enter two numbers :”; cout<<“Enter two numbers :”; cin>>firstnum>>secnum; cin>>firstnum>>secnum; cout<<“\nThe value in first num is: “<<firstnum<<endl; cout<<“\nThe value in first num is: “<<firstnum<<endl; cout<<“The value in secnum is: “<<secnum<<endl; cout<<“The value in secnum is: “<<secnum<<endl; newval(firstnum, secnum); // call the function newval(firstnum, secnum); // call the function cout<< “ The value in firstnum is now: “<<firstnum<<endl; cout<< “ The value in firstnum is now: “<<firstnum<<endl; cout<< “ The value in secnum is now: “<<secnum<<endl; cout<< “ The value in secnum is now: “<<secnum<<endl; return 0; return 0; }

newval function ► void newval(float& xnum, float& ynum) // function header { cout<< “ The value in xnum is: “<<xnum<<endl; cout<< “ The value in xnum is: “<<xnum<<endl; cout<< “ The value in ynum is: “<<ynum<<endl; cout<< “ The value in ynum is: “<<ynum<<endl; xnum = 89.5; xnum = 89.5; ynum = 99.5; ynum = 99.5; return; return; } / * float& xnum read as “ xnum is the address of a floating point value */ / * float& xnum read as “ xnum is the address of a floating point value */

Example 2 ► #include ► #include void calc(float, float, float&, float&); // function prototype void calc(float, float, float&, float&); // function prototype int main() int main() { float firstnum, secnum, thirdnum, sum, product; float firstnum, secnum, thirdnum, sum, product; cout<<“ Enter three numbers: ”; cout<<“ Enter three numbers: ”; cin >>firstnum>>secnum>>thirdnum; cin >>firstnum>>secnum>>thirdnum; calc(firstnum, secnum, thirdnum, sum, product); // function call calc(firstnum, secnum, thirdnum, sum, product); // function call cout<<“\nThe sum of the numbers is: “<<sum<<endl; cout<<“\nThe sum of the numbers is: “<<sum<<endl; cout<<“The product of the numbers is: “<<product<<endl; cout<<“The product of the numbers is: “<<product<<endl; return 0; return 0; }

calc function ► void calc(float num1, float num2, float num3, float& total, float& product) { total = num1 + num2 + num3; total = num1 + num2 + num3; product = num1 * num2 * num3; product = num1 * num2 * num3; return; return; }

Example 3 ( swap 2 values) ► #include ► #include void swap(float&, float&); // function receives 2 references void swap(float&, float&); // function receives 2 references int main() int main() { float firstnum = 20.5, secnum = 6.25; float firstnum = 20.5, secnum = 6.25; cout<<“The value stored in firstnum is: ”<<firstnum<<endl; cout<<“The value stored in firstnum is: ”<<firstnum<<endl; cout<<“The value stored in secnum is : ”<<secnum<<endl; cout<<“The value stored in secnum is : ”<<secnum<<endl; swap(firstnum, secnum); // function call swap(firstnum, secnum); // function call cout<<“The value stored in firstnum is now: ”<<firstnum<<endl; cout<<“The value stored in firstnum is now: ”<<firstnum<<endl; cout<<“The value stored in secnum is now: ”<<secnum<<endl; cout<<“The value stored in secnum is now: ”<<secnum<<endl; return 0; return 0; }

swap function ► void swap(float& num1, float& num2) { float temp; float temp; temp = num1; // save num1’s value temp = num1; // save num1’s value num1 = num2; // store num2’s value num1 = num2; // store num2’s value num2 = temp; // change num2’s value num2 = temp; // change num2’s value return; return; }

Difference between pass by value and pass by reference ► value : value of variable passed reference : address of variable passed reference : address of variable passed ► value : int calsum(int, int, int) // function prototype declaration reference : void swap( float&, float&); reference : void swap( float&, float&); ► value : int calsum(int x, int y, int z) // function header reference : void swap(float& num1, float& num2) reference : void swap(float& num1, float& num2) ► value : copy of value is stored in the called function reference : value changes as the address is referenced reference : value changes as the address is referenced