Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.

Slides:



Advertisements
Similar presentations
Functions 1. Example: Power, Square Root and Absolute values of a number #include … float num; float power, squareRoot, absolute; cout
Advertisements

1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
Chapter 6: User-Defined Functions I
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6: User-Defined Functions I
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
1 CS 1430: Programming in C++. 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
1 Program 2 Pseudo Code Read NumOfSongs of 1 st CD (Prime Read!) While not end of file Process CD Read NumOfSongs for next CD.
Chapter 05 (Part II) Control Statements: Part II.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CS 1430: Programming in C++.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Activation Record int main() { float allScores[MAX_ROWS][MAX_COLS]; int rows, cols; GetAllData(allScores, rows, cols);... } 1 void GetAllData(float a[][MAX_COLS],
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.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Review 1.
Chapter 6: User-Defined Functions I
CS 1430: Programming in C++ No time to cover HiC.
Arrays Outline 1 Introduction 2 Arrays 3 Declaring Arrays
CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017
CS 1430: Programming in C++.
CSCI 161: Introduction to Programming Function
CS 1430: Programming in C++.
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS 1430: Programming in C++ No time to cover HiC.
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.
Arrays Kingdom of Saudi Arabia
CS 1430: Programming in C++ No time to cover HiC.
Counting Loops.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
Arrays Arrays A few types Structures of related data items
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 1430: Programming in C++.
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and update the value of the actual parameter. Value Parameters (No &) The formal parameter receives (a copy of) the value of the actual parameter, and the function can use the value, but can NOT change the value of the actual parameter. How to pass the parameters? 1

In, Out, InOut Parameters In The function uses the value of the actual parameter. Out The calling function gets the new or updated value of the formal parameter. (a function can only return one value using the return statement) InOut Both In and Out Why do we pass parameters? Nothing to do with the return statement. 2

Function Parameters For each parameter 1. Figure out In, Out or InOut 2. Pass by value or Pass by reference In Pass by value (value parameter) (Does not let the function modify the values without &) Out Pass by reference (reference parameter with &) InOut Pass by reference (reference parameter with &) 3

In, Out, InOut Parameters Passing parameters at function call Nothing to do with what the function does. In Parameters Not input inside the function Out Parameter Not output inside the function 4

In, Out, InOut Parameters // // The function inputs payRate and hoursWorked and // pass both values back to calling function // Parameters: (out, out) // void GetInput(float& payRate, float& hoursWorked) { cout << “Enter the pay rate: ”; cin >> payRate; cout << “Enter the hours: ”; cin >> hoursWorked; } The function does input. The parameters are OUT. 5

In, Out, InOut Parameters // // The function displays avg, max and min in required format. // Parameters: (In, In, In) // void DisplayResult(float avg, float max, float min) { cout << fixed << showpoint << setprecision(2); cout << endl << endl << "The average score: " << setw(8) << avg << “.\n” << "The highest score: " << setw(8) << max << “.\n” << "The lowest score : " << setw(8) << min; } The function does output. The parameters are IN. 6

Programming Rules Function description Specify IN, OUT, or INOUT parameters // // The function computes and returns the gross pay based on // the pay rate and hours. Hours over will be paid 1.5 times // the regular pay rate. // Parameters: (in, in) // float GrossPay(float payRate, float hours) { // Function body } // // The function inputs payRate and hoursWorked and // pass both values back to calling function // Parameters: (out, out) // void GetInput(float& payRate, float& hoursWorked) { // Function body } 7

Programming Rules Use Reference parameters only when necessary! // // The function computes and returns the gross pay // based on the pay rate and hours. Hours over 40 will be paid // 1.5 times the regular pay rate. // Parameters: (in, in) // float GrossPay(float& payRate, float& hours) { // Function body } // It will it work. // Lose points! // // The function inputs payRate and hoursWorked and // pass both values back to calling function. // Parameters: (out, out) // void GetInput(float payRate, float hoursWorked) { // Function body } // Will compile and run. // Won’t get values in the calling function. 8

Value Parameter What can the actual parameter be? A VALUE! Literal value (magic number) Variable (initialized!) Expression (with all variables initialized) 9

10 Function Calls // Function Prototype float sqrt(float x); // Function Calls root1 = (-coefB + sqrt(delta)) / (2 * coefA); // Parameter is variable delta with a value cout << “The square root of 10 is “ << sqrt(10); // Parameter is literal value 10 Value = sqrt (coefB * coefB - 4 * coefA * coefC); // Parameter is an expression // with coefA, coefB and coefC initialized

float GrossPay(float payRate, float hours); cin >> hours >> rate; gross = GrossPay(rate, hours); // Valid function call? gross = GrossPay(rate, 35.5); // Valid function call? // Holiday hours gross = GrossPay(2 * rate, hours); // Valid function call? 11

Uninitialized Variable float GrossPay(float payRate, float hours); // Call function with uninitialized variable float gross, hours, rate; gross = GrossPay(rate, hours); HiC Error: HicUndefException: Accessing uninitialized value from rate on line ABNORMAL TERMINATION. 12

Reference Parameter What can the actual parameter be? AN ADDRESS! Literal value: NO Expression : NO Variable : YES initialized or uninitialized float rate, hours; GetInput(rate, hours); // Valid function call? GetInput(rate, 35); // Valid function call? GetInput(2 * rate, hours); // Valid function call? 13

Prog3 Must use functions! 14

Schedule Quiz4-3: Due 9:30 PM today Must use input file! Thursday Lab5: 5 points Prog3 Progress: 2 points Friday Discuss Prog3 Answer all questions Monday, March 21 Drop your Prog3.cpp Wednesday, March 23 Prog3 is due 15

16 Loading an Input File in HiC Save the input file on your drive Click the RUN menu Select option "Set Input File …" Click “Load Input” Browse to the file and open it Select the Input (Interactive) radio button Click OK Use input file for Prog3!

CS 1430: Programming in C++ QuizProg3 17