Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Chapter 7: User-Defined Functions II
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Computer Science 1620 Loops.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Functions:Passing Parameters by Value Programming.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
CS 201 Functions Debzani Deb.
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
Chapter 8 Arrays and Strings
Chapter 6: Functions.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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&
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 8 Arrays and Strings
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Flowcharts.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
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: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Chapter 3: User-Defined Functions I
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
User-Defined Functions (cont’d) - Reference Parameters.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
A Sample Program #include using namespace std; int main(void) { cout
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 7: User-Defined Functions II
What Actions Do We Have Part 1
Chapter 6: Modular Programming
Basic Elements of C++.
User-Defined Functions
Basic Elements of C++ Chapter 2.
Programming Funamental slides
CS150 Introduction to Computer Science 1
Chapter 6: User-Defined Functions I
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Presentation transcript:

Value and Reference Parameters

CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence (NOT)  Array elements as arguments (section 9.3)  Flowchart notation for a user defined function  Structure charts

CSCE 1063 Summary of Value Parameters  Value of corresponding actual argument is stored in the called function  The function execution cannot change the actual argument value  Actual argument can be an expression, variable, or constant  Formal parameter type must be specified in the formal parameter list  Parameters are used to store the data passed to a function

CSCE 1064 Summary of Reference Parameters  Address of corresponding actual argument is stored in the called function  The function execution can change the actual argument value  Actual argument must be a variable only  Formal parameter type must be followed by & in the formal parameter list  Parameters are used to return outputs from the function or to change the value of a function argument

CSCE 1065 Listing 6.2 Functions main and test

CSCE 1066 Argument/Parameter List Correspondence (NOT)  Number: number of actual arguments used in function call must be the same as the number of formal parameters listed in the function header and prototype  Order: Order of arguments in the lists determines correspondence  Type: Each actual argument must be of a data type that can be assigned to the corresponding formal parameter with no unexpected loss of information

CSCE 1067 Array Elements as Arguments  Exercise: Create a C++ function called “exchange” to exchange the contents of two floating-point memory locations.  Can pass array elements to functions exchange (s[3], s[5]);

CSCE 1068 A program module/function is represented in flowcharts by the following special symbol. Flowchart Function Notation

CSCE 1069 The position of the function/module symbol indicates the point the function is executed/called. A separate flowchart should be constructed for the function/module. START END Read Input Call calc_pay function Flowchart Function Notation (cont’d) Display results

CSCE Exercise 1 Draw a flowchart for an algorithm that calculates the average of two numbers by the use of a value returning function (computeAverage). The main function should input the two numbers, as well as outputting the average.

CSCE Solution INPUT n1, n2 OUTPUT average START STOP START STOP average = computeAverage(n1, n2) return (n1 +n2)/2 computeAverage main

CSCE Figure 6.4 Structure chart for general sum and average problem

CSCE // File: computeSumAve.cpp // Computes and prints the sum and average of a collection of data #include using namespace std; // Functions used... // Computes sum of data float computeSum (int);// IN - number of data items // Computes average of data float computeAve (int,// IN - number of data items float);// IN - sum of data items // Prints number of items, sum, and average void printSumAve (int,// IN - number of data items float,// IN - sum of the data float);// IN - average of the data Listing 6.6 main function

CSCE int main( ) { int numItems;// input - number of items to be added float sum;// output - accumulated sum of the data float average;// output - average of data being processed // Read the number of items to process. cout << “Enter the number of itesm to process: “; cin >> numItems; // Compute the sum of the data. sum = computeSum(numItems); // Compute the average of the data. average = computeAve(numItems, sum); // Print the sum and the average. printSumAve(numItems, sum, average); return 0; } Listing 6.6 main function (continued)

CSCE Listing 6.7 Function computeSum // Computes sum of data. // Pre: numItems is assigned a value. // Post: numItems data items read; their sum is stored in sum // Returns: Sum of all data items read if numItems >= 1; //otherwise, 0; float computeSum (int numItems) // IN: number of data items { // Local data... float item;// input: contains current data item float sum;// output: used to accumulate sum of data //read in

CSCE Listing 6.7 Function computeSum (continued) // Read each data item and accumulate it in sum. sum = 0.0; for (int count = 0; count < numItems; count++) { cout << “Enter a number to be added: “; cin >> item; sum += item; } // end for return sum; } // end computeSum

CSCE Listing 6.8 Function computeAve // Computes average of data // Pre: numItems and sum are defined; numItems must be // greater than 0. // Post: If numItems is positive, the average is computed // as sum / numItems. // Returns: The average if numItems is positive; //otherwise, 0; float computeAve (int numItems, // IN: number of data items float sum;// IN: sum of data

CSCE Listing 6.8 Function computeAve (continued) { // Compute the average of the data. if (numItems < 1) // test for invalid input { cout << “Invalid value for numItems = “ << numItems << endl; cout << “Average not computed.” << endl; return 0.0; // return for invalid input } return sum / numItems; } // end computeAve

CSCE Listing 6.9 Function printSumAve // Prints number of items, sum, and average of data. // Pre: numItems, sum, and average are defined. // Post: displays numItems, sum and average if numItems > 0 void printSumAve (int numItems,// IN: number of data items float sum,// IN: sum of the data float average)// IN: average of the data

CSCE Listing 6.9 Function printSumAve (continued) { // Display results is numItems is valid. if (numItems > 0) { cout << “The number of items is “ << numItems << endl; cout << “The sum of the data is “ << sum << endl; cout << “The average of the data is “ << average << endl; } else { cout << “Invalid number of items = “ << numItems << endl; cout << “Sum and average are not defined.“ << endl; cout << “No printing done. Execution terminated.” << endl; } // end if } // end printSumAve

CSCE The following formula gives the distance between two points (x1, y1) and (x2, y2) in the Cartesian plane: (x2 – x1) 2 + (y2 – y1) 2 ) Given the centre and a point on the circle, you can use this formula to find the radius of the circle. Draw the flow charts and write a complete C++ program that prompts the user to enter the centre and a point on the circle. The program should then compute and output the circle's radius, diameter, circumference, and area. Your program must have at least the following user-defined functions:  Distance. This function takes as its parameters four numbers that represent two points in the plane and returns the distance between them.  Radius. This function takes as its parameters four numbers that represent the center and a point on the circle, calls the function Distance to find the radius of the circle, and returns the circle's radius.  Circumference. This function takes as its parameters a number that represents the radius of the circle and returns the circle's circumference. (If r is the radius, the circumference is 2  r)  Area. This function takes as its parameters a number that represents the radius of the circle and returns the circle's area. ((If r is the radius, the area is  r 2 )  Assume  = Please use a structure chart to help you analyse the given exercise. Exercise 2

CSCE Next lecture will be about Arrays as Function Parameters