User-Defined Functions (cont’d) - Reference Parameters.

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

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
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.
1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Functions:Passing Parameters by Value Programming.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
CS 201 Functions Debzani Deb.
Chapter 6: Functions.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
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).
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Functions g g Data Flow g Scope local global part 4 part 4.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
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.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
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.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.
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.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Passing Objects to Methods
Chapter 6: Modular Programming
A Lecture for the c++ Course
Chapter 5 Functions DDC 2133 Programming II.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Value returning Functions
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.
Review for Final Exam.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Function “Inputs and Outputs”
Review for Final Exam.
The Function Prototype
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
CS150 Introduction to Computer Science 1
Functions, Part 2 of 42 Topics Functions That Return a Value
Programming Fundamental
ICS103: Programming in C 6: Pointers and Modular Programming
Presentation transcript:

User-Defined Functions (cont’d) - Reference Parameters

CSCE 1062 Outline  Exercise  Passing by value  Scope of variables  Reference parameters (section 6.1)

CSCE 1063 Tracing Exercise #include using namespace std; int Blend( int red, int green ); // prototype void main() { int red = 5, blue = 3; blue = Blend(blue, red); cout << red << ' ' << blue << '\n'; blue = Blend(red, blue); cout << red << ' ' << blue << '\n'; } int Blend( int red, int green ) // parameters passed by value { int yellow;// local variable in Blend function cout << “enter Blend “ << red <<‘ ‘ << green << ‘\n’; yellow = red + green; cout << “leave Blend “ << red <<‘ ‘ << green << ‘\n’; return (yellow + 1); } Execution

CSCE 1064 Passing by Value main function data area actual arguments: red 5 blue 3 Blend function data area (1 st call) formal arguments: red 3 green 5 Local arguments: yellow 8

CSCE 1065 Tracing Exercise (cont’d) main Blend (1 st call) Blend (2 nd call) redblue Output: enter Blend 3 5 leave Blend enter Blend 5 9 leave Blend redgreenyellow 358 redgreenyellow 5914

CSCE 1066 Scope of Variables  Scope - where a particular meaning of a variable identifier is visible or can be referenced  Local - can be referred to only within a program segment or function  In a program segment (localized declarations of variables) for (int i = 1; i <= 10; ++i) cout << “*”; Commonly used for loop control variables Declared at point of first reference Value has meaning (i.e. can be referenced) only inside loop segment.

CSCE 1067 Scope of Variables (cont’d)  In a function this applies to formal argument names constants and variables declared within the function  Global - can be referred to within all functions  useful especially for constants  must be used with care

CSCE 1068 Listing 3.15 Outline of program for studying scope of variables

CSCE 1069 Scope of Variables (cont’d)  Global variables MAX & LIMIT are visible to main, one, funTwo functions  Local variable localVar in main function visible only to main function  Local variables anArg, second & oneLocal in one function are visible only to one function  Local variables one, anArg & localVar in funTwo function are visible only to funTwo function

CSCE Passing by Reference Example #include using namespace std; int Blend( int& red, int green ); // prototype void main() { int red = 5, blue = 3; Blend(blue, red); cout << red << ' ' << blue << '\n'; Blend(red, blue); cout << red << ' ' << blue << '\n'; } void Blend(int& red, int green) // green parameter passed by value, // while red parameter passed by reference { int yellow;// local variable in Blend function cout << “enter Blend “ << red <<‘ ‘ << green << ‘\n’; yellow = red + green; cout << “leave Blend “ << red <<‘ ‘ << green << ‘\n’; red = yellow + 1; }

CSCE Passing by Reference main function data area actual arguments: red 5 blue 3 Blend function data area (1 st call) formal arguments: red address of blue green 5 Local arguments: yellow 8

CSCE Passing by Reference Example (cont’d) main Blend (1 st call) Blend (2 nd call) redblue Output: enter Blend 3 5 leave Blend enter Blend 5 9 leave Blend redgreenyellow 58 redgreenyellow 914

CSCE User Defined Functions Can:  return no value  type void  return exactly one value  function type  return statement  return more than one value  type void or function type  reference parameters

CSCE Reference Parameters  Formal parameter data type directly followed by & indicate a parameter passed by reference

CSCE Listing 6.1 Function to compute sum and average

CSCE Listing 6.1 Function to compute sum and average (continued)

CSCE User-Defined Function computeSumAve  Two function input parameters  num1, num2  Two function output parameters  sum, average  & indicates function output parameters  Function call computeSumAve(x, y, sum, mean);

CSCE Argument Correspondence Actual Argument x y sum mean Corresponds to Formal Argument num1 (fn. input) num2 (fn. input) sum (fn. output) average (fn. output)

CSCE Call-by-Value and Call-by- Reference Parameters  & between type and identifier defines a parameter as function output mode (pass by reference)  no & in a parameter’s declaration identifies parameter as fuction input mode (pass by value)  Compiler uses information in parameter declaration list to set up correct argument-passing mechanism

CSCE Figure 6.1 Data areas after call to computeSumAve (before execution)

CSCE Figure 6.2 Data areas after execution of computeSumAve

CSCE Notes on Call-by-Reference  Place the & only in the formal parameter list - not in the actual parameter list  Place the & also in the prototype: void computeSumAve(float, float, float&, float&);  Note that this is a void function

CSCE When to Use a Reference or a Value Parameter  If information is to be passed into a function and doesn’t have to be returned or passed out of the function, then the formal parameter representing that information should be a value parameter (function input parameter).  If information is to be returned to the calling function through a parameter, then the formal parameter representing that information must be a reference parameter (function output parameter).

CSCE When to Use a Reference or a Value Parameter (cont’d)  If information is to be passed into a function, perhaps modified, and a new value returned, then the formal parameter representing that information must be a reference parameter (input/output parameter)

CSCE Next lecture we will talk more about Value and Reference Parameters