Void Functions Call-By-Reference Parameters Using Procedural Abstraction Testing and Debugging General Debugging Techniques

Similar documents
Chapter void Functions. Overview. Functions for All Subtasks. void-functions. Using a void-function. void-function Definition

Call- by- Reference Func0ons Procedural Abstrac0ons Numerical Conversions CS 16: Solving Problems with Computers I Lecture #9

Programmer-Defined Functions

C++ Basics - 3 Rahul

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

Chapter 3 Function Basics

More on Func*ons Command Line Arguments CS 16: Solving Problems with Computers I Lecture #8

Function Call Example

Computer Programming

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

Lab Instructor : Jean Lai

CSCI 1061U Programming Workshop 2. Function Basics

CSCI 123 Introduction to Programming Concepts in C++

Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

4. C++ functions. 1. Library Function 2. User-defined Function

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

C++ Programming: From Problem Analysis to Program Design, Third Edition

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

Fundamentals of Programming CS-110. Lecture 2

Lecture 23: Pointer Arithmetic

Introduction to Programming EC-105. Lecture 2

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Computer Programming

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Chapter 16. Templates. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Problem Solving with C++

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Functions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;

Absolute C++ Walter Savitch

Chapter 5. Functions for All Subtasks. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

EECS402 Lecture 02. Functions. Function Prototype

Module Contact: Dr Pierre Chardaire, CMP Copyright of the University of East Anglia Version 1

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

C++ For Science and Engineering Lecture 15

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

do { statements } while (condition);

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Chapter 1 Style Requirements

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

What is a Function? What are functions good for?

A SHORT COURSE ON C++

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

CSC102 INTRO TO PROGRAMMING WITH PYTHON LECTURE 27 CRASH COURSE IN C++ MICHAEL GROSSBERG

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Chapter 3. Numeric Types, Expressions, and Output

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Chapter 6 Sub Procedures

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Chapter 7 - Notes User-Defined Functions II

Using Free Functions

Arrays in C++ Instructor: Andy Abreu

Unit 14. Passing Arrays & C++ Strings

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lab # 02. Basic Elements of C++ _ Part1

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

Review of Important Topics in CS1600. Functions Arrays C-strings

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Local and Global Variables

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

CSCE 110 PROGRAMMING FUNDAMENTALS

I/O Streams and Standard I/O Devices (cont d.)

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

BITG 1113: Function (Part 2) LECTURE 5

Programming Language. Functions. Eng. Anis Nazer First Semester

C++ basics Getting started with, and Data Types.

Chapter 4. Procedural Abstraction and Functions That Return a Value

Chapter Procedural Abstraction and Functions That Return a Value. Overview. Top-Down Design. Benefits of Top Down Design.

Developed By : Mrs. K. M. Sanghavi

Programming challenges in C++

VOLUME II CHAPTER 9 INTRODUCTION TO C++ HANDS ON PRACTICE PROGRAMS

Chapter 2 C++ Fundamentals

2.1-First.cpp #include <iostream> // contains information to support input / output using namespace std;

Outline. Introduction to Programming (in C++) Introduction. First program in C++ Programming examples

SOLUTIONS TO EXERCISES PART ONE: Problem-Solving Techniques

Lecture # 6. Repetition. Review. If Else Statements Comparison Operators Boolean Expressions Nested Ifs. Switch Statements.

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

3.2 Predefined Functions Libraries. 3.1 Top Down Design. 3.2 Predefined Functions Libraries. 3.2 Function call. Display 3.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

WARM UP LESSONS BARE BASICS

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons


Computer Science II Lecture 1 Introduction and Background

Transcription:

Chapter 5

In this chapter, you will learn about: Void Functions Call-By-Reference Parameters Using Procedural Abstraction Testing and Debugging General Debugging Techniques

void Functions Recall a Function implements subtask In General a Subtask can return: Only one value --- See Chapter 4 No value (e.g. display GUI on screen) or Multiple values How do you implement a subtask that returns no value? Use void function

void Function Definition Example No value is returned by show_results void show_results(double f_degrees, double c_degrees) using namespace std; cout << f_degrees << degrees Fahrenheit is euivalent to << endl << c_degrees << degrees Celsius. << endl; return; No expression in return statement

Declaration void Function_name (Parameter List); Insert Function Declaration Comments here void Function_name (Parameter List) Function Definition Declaration_1 Declaration_1 May (or may not) include one or more return statements. return is optional Declaration_Last Executable_Statement_1 Executable_Statement_2 Executable_Statement_Last

void Function calls Consider: void show_results(double f_degrees, double c_degrees);. void show_results(double f_degrees, double c_degrees) using namespace std; cout << f_degrees << degrees Fahrenheit is euivalent to << endl; << c_degrees << degrees Celsius. << endl; return; What is wrong with this statement? Example call show_result(32.5, 0.3); Does not require leading = sign used for assignment cout << show_result(325, 0.3)

void Function Program Analysis-I 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 void initialize_screen( ); // Separates current output from the output of previous run double celsius (double fahrenheit); // Converts Fahrenheit to Celsius void_show_results (double f_degrees, double c_degrees); // Displays output, Assume c_degrees is equivalent to f_degrees int main( ) using namespace std; double f_temp, c_temp; initialize screen( ); cout << Converts Fahrenheit to Celsius\n << Enter Fahrenheit temp cin << f_temp; c_temp = celsius(f_temp); show_results (f_temp, c_temp); return; void initialize_screen ( ) using namespace std; cout << endl; return; 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 double celsius (double fahrenheit) return ( (5.0/9.0) * (fahrenheit 32)); void show_results (double f_degrees, double c_degrees) using namespace std; cout.setf(ios :: fixed); cout.setf(ios:: showpoint); cout.precision(1); cout << f_degrees << degrees Fahrenheit equals << c_degrees<< Celsius\n ; return;

void Function Program Analysis-II 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 void initialize_screen( ); // Separates current output from the output of previous run double celsius (double fahrenheit); // Converts Fahrenheit to Celsius void_show_results (double f_degrees, double c_degrees); // Displays output, Assume c_degrees is equivalent to f_degrees int main( ) using namespace std; double f_temp, c_temp; initialize screen( ); cout << Converts Fahrenheit to Celsius\n << Enter Fahrenheit temp cin << f_temp; c_temp = celsius(f_temp); show_results (f_temp, c_temp); return; void initialize_screen ( ) using namespace std; cout << endl; return; 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 No Formal Parameters Arguments double celsius (double fahrenheit) return ( (5.0/9.0) * (fahrenheit 32)); void show_results (double f_degrees, double c_degrees) using namespace std; cout.setf(ios :: fixed); cout.setf(ios:: showpoint); cout.precision(1); cout << f_degrees << degrees Fahrenheit equals << c_degrees<< Celsius\n ; return; Optional

void Function Use of return Statement The return statement is optional in a void function definition Other times (when embedded in a conditional statement), it could prevent errors such as division by zero

Call-by-Reference Parameters Changing Multiple Values Allows a Function to change Variables used in a function call Variables to be changed are uniquely identified in the function declaration and definition statements Pertinent Arguments in the calling function must be variables not values (e.g.,numbers)

Function Declaration Call-by-Reference Parameter Example void get_input(double& f_variable); Identifies f_variable as a call-by-reference parameter

Function Definition Call-by-Reference Parameter Example void get_input(double& f_variable) using namespace std; cout << Convert a Fahrenheit temperature << to Celsius.\n << Enter a temperature in Fahrenheit: ; cin >> f_variable; Identifies f_variable as a call-by-reference parameter How do you implement a subtask that changes one or more values?

Function Calls Call-by-Reference Parameter Consider void get_input(double& f_variable); void get_input(double& f_variable) using namespace std; cout << Convert a Fahrenheit temperature << to Celsius.\n << Enter a temperature in Fahrenheit: ; cin >> f_variable; Example call get_input (temp) Memory location of temp is plugged into f_variable. Not its contents

Call-by-Reference Program Analysis-I 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 void get_numbers (int& input1, int& input2); // Reads two integers from keyboard void swap_values( int& variable1, int& variable2); // swaps the values of variable1 and variable2 void_show_results (int output1, int output2) // Displays output1 and output2 sequentially int main( ) int first_num, second_num; get_numbers(first_num, second_num); swap_values(first_num, second_num); show_results(first_num, second_num); return 0; void get_numbers (int& input1, int& input2); using namespace std; cout << Enter two integers ; cin >> input1; cin >> input2; return; 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 void swap_values (int& variable1, int& variable2) int temp; temp = variable1; variable1 = variable2; variable2 = temp; void show_results (int output1, int output2) using namespace std; cout << In reverse order the << numbers are: << output1 << << output2 << endl; return;

Call-by-Reference vs. Call-by-Value Comparison Assume Memory Locations: Call-by-Value Parameter void get_number (int input1); Name Address Contents second_num last_num 1001 1002 10 100 get_numbers(first_num); first_num 1006 20 Call-by-Reference Parameter: void get_number (int& input1); get_numbers(first_num);

Mixed Parameters Reference & Value Parameter You can mix Call-by-Reference and Call-by Value in the same function Example: void cash (double& principal, double& time, double rate) principal and time are call-by-reference formal parameter Changes in principal and time change the argument variables rate is a call-by-value formal parameter Changes in rate do not change the argument variable

How do you decide when to use a call-byreference or call-by-value formal parameter? Does the function need to change the value of the variable used as an argument? If yes then use a call-by-reference formal parameter Otherwise use a call-by-value formal parameter

A Function body may contain a call to another function provided The called function is already declared Do not pass a function call as an argument to another function (Follow the KIS principle) You cannot define a function within the body of another function Why?

void order(int& input1, int& input2); // Orders the numbers in variables input1 and input2 void swap_values(int& variable1, int& variable2); // interchange the values of variables1 and varaibles2 void swap_values(int&, variable1, int& variable2) int temp; temp = variable; variable1 = variable2; variable2 = temp; void order (int& n1, int& n2) This function order( ) calls another function if ( n1 > n2) swap_values(n1, n2);

How to write Function Declaration Comment? Method 1: Refer to Lectures notes in Chap 4 Alternative Method: Break Comments into two parts Preconditions What is assumed to be true when function is called Postconditions The effect of calling the function Let s examine both concept for practical subtasks - real systems

double celsius(double farenheit); //Precondition: fahrenheit is a temperature expressed // degrees Fahrenheit //Postcondition: Returns the equivalent temperature // expressed in degrees Celsius Refer to slide 29 in Chapter 4 lectures notes

Give a precondition and a postcondition for the predefined function sqrt, which returns the square root of its argument: Assume function declaration: double sqrt (double n);

Assuming function declaration: double sqrt (double n); //Precondition: n >= 0 //Postcondition: Returns the square root of n

!! Unit Testing Test each function as a separate unit from the rest of the program It s a lot easier to debug your program suite How do you test individual functions? Create a driver program to test each function Provide the means to check, inspect value(s) returned/changed by the function for different scenarios After a function is unit tested, you can use it as a driver program for untested functions A driver program is written for the sole purpose of testing a function

! How do you test a function that makes a call to other untested function(s)? Create a Stub program for the other untested function The Stub program should simply return values that you know are correct A Stub is a simplified version of a function, which is used in place of the untested function so that the called functions can be tested

! Write a stub for the function with the following declaration: double rain_prob (double pressure, double humidity, double temp); // Precondition: pressure is the baramotic pressure in // inches of mercury, // humidity is the relative humidity as a percent, and // temp is the temperature in degrees Fahrenheit // Returns: the probability of rain, which is a number // between 0 and 1. 0 means no chance of rain, 1 means rain is 100% certain

! "#$ double abc(double p, double q, double r); int main ( ) declarations_1 double result; declarations_last result = abc( x, y, z); - - - - -- - - - - - - - - - - double abc(double x, doubley, double z) Local variable Declarations.. double cahnce; computations chance = rain_prob (double pressure, double humidity, double temp); computations We need a stub for rain_prob( ) in order to unit test abc ( )

! "#$ double rain_prob (double pressure, double humidity, double temp) return 0; // Insert comment here double rain_prob (double pressure, double humidity, double temp) return 0.5; // insert comment here double rain_prob (double pressure, double humidity, double temp) return -1; //Insert comment here double rain_prob (double pressure, double humidity, double temp) return 100; //Insert comment here

All subtasks can be implemented as functions In call-by-value substitution, the value of an argument is plugged into formal parameter In a call-by-reference substitution, the argument should be a variable, and the variable (i.e., its address) is plugged into the formal parameter Function declaration comments can be stated as precondition and postcondition Developer performs unit testing, while Verification Engineer performs Systems/Inter-Systems Testing A driver program has one task - test a function A stub is used in place of a function that is untested