Chapter 5 Functions for All Subtasks 1

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Functions  Programmer-Defined Functions  Local Variables in Functions  Overloading Function Names  void Functions,  Call-By-Reference Parameters in.
Chapter 3 Function Basics Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 ACS 168 Structured Programming Using the Computer Chapter 4 by Joaquin Vila Prepared by Shirley White.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Chapter 6: Functions.
© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Functions for All Subtasks.
Chapter 5 Functions for All Subtasks. Slide 5- 2 Overview 5.1 void Functions 5.2 Call-By-Reference Parameters 5.3 Using Procedural Abstraction 5.4 Testing.
Copyright © 2015 Pearson Education, Ltd.. All rights reserved. Chapter 5 Functions for All Subtasks.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
1 Functions Chapter 7 2 Hope you can function! What is R2D2 doing here? What is his function? Who is Nibble? Can he function? IS he a function? Who is.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
6/4/2016 CSI Chapter 04 1 Design style Indentation makes your program easier to read! You will get a point or two deducted if you do not follow.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Functions for All Subtasks SLO1.8 Explain the benefits of dividing large programming tasks.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Fundamental Programming Fundamental Programming Introduction to Functions.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
COMP 2710 Software Construction C++ Basics 2 and Exercises Dr. Xiao Qin Auburn University These slides.
Function Parameters and Overloading Version 1.0. Topics Call-by-value Call-by-reference Call-by-address Constant parameters Function overloading Default.
Bill Tucker Austin Community College COSC 1315
Repetitive Structures
Pointers and Dynamic Arrays
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
Data Types and Expressions
Functions for All Subtasks
Data Types and Expressions
Basics (Variables, Assignments, I/O)
Chapter 5 Functions for All Subtasks 1
Chapter 6: Modular Programming
Basic Elements of C++.
Chapter 7 Top-Down Development
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Chapter 1. Introduction to Computers and Programming
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Basics (Variables, Assignments, I/O)
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
Functions for All Subtasks
Basic Elements of C++ Chapter 2.
Multiple Files Revisited
Basics (Variables, Assignments, I/O)
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.
6 Chapter Functions.
Parameters and Overloading
Chapter 3 Input output.
A First Book of ANSI C Fourth Edition
Functions Review Programmer-Defined Functions
Chapter 6: User-Defined Functions I
Objectives You should be able to describe: The while Statement
Fundamental Programming
Multiple Files Revisited
Predefined Functions Revisited
Classes, Objects and Methods
C++ Programming Basics
Presentation transcript:

Chapter 5 Functions for All Subtasks 1 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1

Overview 5.1 void Functions 5.2 Call-By-Reference Parameters 5.3 Using Procedural Abstraction 5.4 Testing and Debugging Slide 5- 2 2

5.1 void Functions Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3

void-Functions In top-down design, a subtask might produce No value (just input or output for example) One value More than one value So far we have seen how to implement functions that return one value A void-function implements a subtask that returns no value or more than one value Slide 5- 4 4

Example: Converting Temperatures The functions just developed can be used in a program to convert Fahrenheit temperatures to Celcius using the formula C = (5/9) (F – 32) Do you see the integer division problem? Slide 5- 5 5

Program to convert F to C (hint….) Function double celsius(double fahrenheit) { return ((5.0/9.0)*(fahrenheit - 32)); }

A function to display the conversion results Function desired: show_results Inputs: double f_degrees double c_degrees Function operation: Displays: “x degrees Fahrenheit is equivalent to y degrees Celsius. Returns: nothing

void-Function Definition void show_results(double f_degrees, double c_degrees) { using namespace std; cout << f_degrees << “ degrees Fahrenheit is equivalent to “ << endl << c_degrees << “ degrees Celsius.” << endl; return; } Slide 5- 8 8

void-Function Definition Two main differences between void-function definitions and functions that return a single value: Keyword void replaces the type of the value returned void means that no value is returned by the function The return statement does not include an expression Slide 5- 9 9

Using a void-Function void-function calls are executable statements They do not need to be part of another statement They end with a semi-colon Example: show_results(32.5, 0.3); NOT: cout << show_results(32.5, 0.3); Slide 5- 10 10

Void Function Example Temperature http://ideone.com/0Do0Pm Wiki Code

void-Functions Why Use a Return? Is a return-statement ever needed in a void-function since no value is returned? Yes! What if a branch of an if-else statement requires that the function ends to avoid producing more output, or creating a mathematical error? void-function in Display 5.3, avoids division by zero with a return statement Slide 5- 12 12

Dividing Ice Cream void ice_cream_division(int number, double total_weight) { using namespace std; double portion; if (number == 0) return; portion = total_weight/number; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "Each one receives " << portion << " ounces of ice cream." << endl; }

The Main Function The main function in a program is used like a void function…do you have to end the program with a return-statement? Because the main function is defined to return a value of type int, the return is needed C++ standard says the return 0 can be omitted, but many compilers still require it Slide 5- 14 14

Section 5.1 Conclusion Can you Describe the differences between void- functions and functions that return one value? Tell what happens if you forget the return- statementin a void-function? Distinguish between functions that are used as expressions and those used as statements? Slide 5- 15 15

5.2 Call-By-Reference Parameters 16 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 16

Call-by-Value A new local variable f has the value of f_temperature copied to it c_temperature = celsius(f_temperature); copy double celsius(double f) return ((5.0/9.0)*(f - 32)); { }

Call-by-Reference Parameters A return only allows the return of a single value. With Call-by-value we can use the formal parameter like a variable and change them, but the values don't propagate back to the calling routine. Call-by-reference parameters allow us to change the variable used in the function call, and the change changes the value in the calling routine Arguments for call-by-reference parameters must be variables, not numbers or expressions. Slide 5- 18 18

Call-by-Reference Example Need to get two values. void get_numbers(int& input1, int& input2) { using namespace std; cout << "Enter two integers: "; cin >> input1 >> input2; } ‘&’ symbol (ampersand) identifies input1 and input2 as a call-by-reference parameters Used in both declaration and definition! Slide 5- 19 19

Call-by-Reference Example 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; }

Call-by-Reference Changing input1 actually changes first_number! int first_num, second_num; get_numbers(first_num,second_num); reference reference void get_numbers(int& input1, int& input2) { … cin >> input1; cin >> input2; } Input1 and input2 merely refer back the the variables in the main program

Call-by-Value A new local variable f has the value of f_temperature copied to it c_temp = celsius(f_temp); copy If we change f, it will have no impact on f_temp double celsius(double f) return ((5.0/9.0)*(f - 32)); { }

Call-By-Reference Details Call-by-reference works almost as if the argument variable is substituted for the formal parameter, not the argument’s value In reality, the memory location of the argument variable is given to the formal parameter Whatever is done to a formal parameter in the function body, is actually done to the value at the memory location of the argument variable Slide 5- 23 23

Call-By-Value Details void show_results(int output1, int output2) { using namespace std; cout << "In reverse order the numbers are: " << output1 << " " << output2 << endl; } . . . get_numbers(first_num, second_num); swap_values(first_num, second_num); show_results(first_num, second_num); output1 Memory 34 56 54 output2 first_num second_num Slide 5- 24 24

Call-By-Reference Details void swap_values(int& variable1, int& variable2) { int temp; temp = variable1; variable1 = variable2; variable2 = temp; } . . . get_numbers(first_num, second_num); swap_values(first_num, second_num); show_results(first_num, second_num); variable1 Memory 34 56 variable2 first_num second_num Slide 5- 25 25

Mixed Parameter Lists Call-by-value and call-by-reference parameters can be mixed in the same function Example: void good_stuff(int& par1, int par2, double& par3); par1 and par3 are call-by-reference formal parameters Changes in par1 and par3 change the argument variable par2 is a call-by-value formal parameter Changes in par2 do not change the argument variable Slide 5- 26 26

Parameter Types Example http://ideone.com/uhZZue Slide 5- 27 27

Inadvertent Local Variables If a function is to change the value of a variable the corresponding formal parameter must be a call-by-reference parameter with an ampersand (&) attached Forgetting the ampersand (&) creates a call-by-value parameter The value of the variable will not be changed The formal parameter is a local variable that has no effect outside the function Hard error to find…it looks right! Slide 5- 28 28

Section 5.2 Conclusion Can you Write a void-function definition for a function called zero_both that has two reference parameters, both of which are variables of type int, and sets the values of both variables to 0. Write a function that returns a value and has a call-by-reference parameter? Write a function with both call-by-value and call-by-reference parameters Slide 5- 29 29

5.3 Using Procedural Abstraction 30 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 30

Using Procedural Abstraction Functions should be designed so they can be used as black boxes To use a function, the declaration and comment should be sufficient Programmer should not need to know the details of the function to use it Slide 5- 31 31

Functions Calling Functions A function body may contain a call to another function The called function declaration must still appear before it is called Functions cannot be defined in the body of another function void order(int& n1, int& n2) { if (n1 > n2) swap_values(n1, n2); } swap_values called if n1 and n2 are not in ascending order After the call to order, n1 and n2 are in ascending order http://ideone.com/nz81tx Slide 5- 32 32

Pre and Postconditions Precondition States what is assumed to be true when the function is called Function should not be used unless the precondition holds Postcondition Describes the effect of the function call Tells what will be true after the function is executed (when the precondition holds) If the function returns a value, that value is described Changes to call-by-reference parameters are described Slide 5- 33 33

swap_values revisited Using preconditions and postconditions the declaration of swap_values becomes: void swap_values(int& n1, int& n2); //Precondition: variable1 and variable 2 have // been given values // Postcondition: The values of variable1 and // variable2 have been // interchanged Slide 5- 34 34

Function celsius Preconditions and postconditions make the declaration for celsius: double celsius(double farenheit); //Precondition: fahrenheit is a temperature // expressed in degrees Fahrenheit //Postcondition: Returns the equivalent temperature // expressed in degrees Celsius Slide 5- 35 35

Why use preconditions and postconditions? should be the first step in designing a function specify what a function should do Always specify what a function should do before designing how the function will do it Minimize design errors Minimize time wasted writing code that doesn’t match the task at hand Slide 5- 36 36

Time Converter Problem Write a program to convert military time to standard notion 15:34 Military 3:34pm Standard

Time Converter Problem int main() { int m_hour, s_hour, minutes; char repeat, ampm; cout << "This program converts 24-hour time to” cout << "standard (AM/PM) time.\n\n"; input_time(m_hour, minutes); convert_time(m_hour, s_hour, ampm); output_time(m_hour, s_hour, minutes, ampm); }

Time Converter Problem input_time(m_hour, minutes); //Precondition: none //Prompts user to enter values for the current time, in 24-hour notation. //Postcondition: hour and min have been set to values entered by the user, //with hour filtered to be between 0 and 23 and min between 0 and 59.

Time Converter Problem convert_time(m_hour, s_hour, ampm); //Precondition: mil_hour is between 0 and 23. //Postcondition: std_hour is coverted to the appropriate hour 1-12 assuming //that mil_hour was a military hour. ap is also set to 'A' or 'P' depending //on whether the standard hour is AM or PM.

Time Converter Problem output_time(m_hour, s_hour, minutes, ampm); //Precondition: mil_hour is a number between 0 and 23, and std_hour and ap //are set to the hour and character ('A' for am, 'P' for pm) corresponding to //the value of mil_hour interpreted in 24-hour time. //Postcondition: The times in both military and standard time have been sent //to the screen nicely formatted.

Time Converter Problem http://ideone.com/KiUhBc