Chapter 4 Procedural Abstraction and Functions That Return a Value 1

Slides:



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

Copyright © 2003 Pearson Education, Inc. Slide 1 Created by David Mann, North Idaho College.
Copyright © 2002 Pearson Education, Inc. Slide 1.
User Defined Functions
Chapter Five Functions
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
1 Walter Savitch Chapter 3 Procedural Abstraction and Functions That Return a Value Slides by David B Teague, Western Carolina University, A constituent.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Functions  Programmer-Defined Functions  Local Variables in Functions  Overloading Function Names  void Functions,  Call-By-Reference Parameters in.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Copyrights ©2005 Pearson Addison-Wesley. All rights reserved. 1 Chapter 4 Procedural Abstraction and Functions That Return a Value.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 6: User-Defined Functions I
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Chapter 3 Procedural Abstraction and Functions That Return a Value.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Procedural Abstraction and Functions That Return a Value.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Top-Down Design SLO1.8 Explain the benefits of dividing large programming tasks to smaller.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Functions. Let’s look at the 2 programs of evaluating: y=2^3+2^5+2^6; #include using namespace std; int main() { int y=0; int partResult=1; for (int i=1;
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Copyright © 2015 Pearson Education, Ltd.. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
Chapter 3.1 & 3.2 s Programming s Assignment Statements s Incrementing & Decrementing s Math Library Functions.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015 Pearson Education, Ltd.. All rights reserved. Chapter 4 Procedural Abstraction and Functions That Return a Value.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 Functions.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
Chapter 6: User-Defined Functions I
Procedural Abstraction and Functions That Return a Value
Basics (Variables, Assignments, I/O)
Chapter 5 Functions for All Subtasks 1
BASIC ELEMENTS OF A COMPUTER PROGRAM
Procedural Abstraction and Functions That Return a Value
Procedural Abstraction and Functions That Return a Value
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Function Basics TBC=15 Dr. Xiao Qin.
CMPT 201 Functions.
Basics (Variables, Assignments, I/O)
User-Defined Functions
Basics (Variables, Assignments, I/O)
User Defined Functions
Functions October 23, 2017.
Chapter 3 Even More Flow of Control 1
Functions Review Programmer-Defined Functions
Chapter 6: User-Defined Functions I
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 144 Advanced C++ Programming January 31 Class Meeting
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Presentation transcript:

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

Overview 4.1 Top-Down Design 4.2 Predefined Functions 4.3 Programmer-Defined Functions Slide 4- 2 2

4.1 Top-Down Design Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 3

Top Down Design To write a program Develop the algorithm that the program will use Translate the algorithm into the programming language Top Down Design (also called stepwise refinement) Break the algorithm into subtasks Break each subtask into smaller subtasks Eventually the smaller subtasks are trivial to implement in the programming language Slide 4- 4 4

Top Down Design

Average Calculator Average Calculator Get Marks Calculate Average Output Average Count marks Sum marks Divide Sum by count

Benefits of Top Down Design Subtasks, or functions in C++, make programs Easier to understand Easier to change Easier to write Easier to test Easier to debug Easier for teams to develop Slide 4- 7 7

4.2 Predefined Functions Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 8

Predefined Functions C++ comes with libraries of predefined functions Example: sqrt function the_root = sqrt(9.0); Argument Variable will be 3 Function Slide 4- 9 9

Function call Example Program to compute dog house size from available funds: http://ideone.com/EI4rje

Function Calls A function call can be used like any expression bonus = sqrt(sales) / 10; Cout << “The side of a square with area “ << area << “ is “ << sqrt(area); Slide 4- 11 11

Function Call Syntax Function_name (Argument_List) Argument_List is a comma separated list: (Argument_1, Argument_2, … , Argument_Last) Example: side = sqrt(area); cout << “2.5 to the power 3.0 is “ << pow(2.5, 3.0); Slide 4- 12 12

Function Libraries Predefined functions are found in libraries The library must be “included” in a program To include the math library containing sqrt(): #include <cmath> Newer standard libraries, such as cmath, also require the directive using namespace std; Slide 4- 13 13

Other Predefined Functions abs(x) --- int value = abs(-8); Returns absolute value of argument x Return value is of type int Argument is of type int Found in the library cstdlib fabs(x) --- double value = fabs(-8.0); Returns the absolute value of argument x Return value is of type double Argument is of type double Found in the library cmath Slide 4- 14 14

Type Casting Recall the problem with integer division: int totalCandy = 9, numberOfPeople = 4; double candyPerPerson; candyPerPerson = totalCandy / numberOfPeople; candyPerPerson = 2, not 2.25! A Type Cast produces a value of one type from another type static_cast<double>(totalCandy) produces a double representing the integer value of totalCandy Slide 4- 15 15

4.3 Programmer-Defined Functions 19 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 19

User Created Function Program to compute the total cost of a bulk purchase. TaxRate = 0.05 (5%) Given a quantity and price per item, compute the total payment as: subTotal = quantity * price total = subTotal + subTotal * TaxRate http://ideone.com/h8KuYi

Function quantity Total_Cost Function total_cost cost

Function Declaration Parameter type Parameter Return type double total_cost(int number_par, double price_par); Return type Function name Parameter list Semicolon

Function Definition Function head Function body Function return double total_cost(int number_par, double price_par) { const double TAX_RATE = 0.05; //5% sales tax double subtotal; subtotal = price_par * number_par; return (subtotal + subtotal*TAX_RATE); } Function head Function body Function return

Order of Arguments Compiler checks that the types of the arguments are correct and in the correct sequence. Compiler cannot check that arguments are in the correct logical order Slide 4- 24 24

Function Definition Syntax Within a function definition Variables must be declared before they are used Variables are typically declared before the executable statements begin At least one return statement must end the function Each branch of an if-else statement might have its own return statement 25

Placing Definitions A function call must be preceded by either The function’s declaration or The function’s definition If the function’s definition precedes the call, a declaration is not needed Placing the function declaration prior to the main function and the function definition after the main function leads naturally to building your own libraries in the future. Slide 4- 26 26

Hands On (Use Functions) Goal – write a program that will: Allow several test scores to be entered (number of scores determined by the user (10 Max) Display the average of the test scores. Display a grade base on the following table: A score >=90 B 80 <= score < 90 C 70 <= score < 80 D 60 <= score < 70 F score < 60

Exam Grade Program How to design? Break down problem using stepwise refinement Define the functions. Write and test each function Make mainline, test it all.

Get Grades and compute average Exam Grade Program Grade Program Get Grades and compute average Compute Letter grade Display Letter Grade

Functions double readAndAverageScores(int maxScores); char computeGrade(double average); void display Results(char grade);

Function Activity Can you Write a double function Rectangle that takes two double parameters width and height, and returns the area. Write a double function Triangle that takes two double parameters width and height, and returns the area. Write a program that asked the use for a height and width, and a type ('T' for triangle, 'R' for rectangle), and returns the area of that type thing. Slide 4- 31 31