Procedural Abstraction and Functions That Return a Value. Savitch, Chapter 4

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

Function Call Example

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

Using Free Functions

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

Chapter 4. Procedural Abstraction and Functions That Return a Value

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

C++ Programming Lecture 11 Functions Part I

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

Preview from Notesale.co.uk Page 2 of 79

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

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

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

6: Initialization & Cleanup

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi

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

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

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

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

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Benefits of Methods. Chapter 5 Methods

Chapter 3 Function Overloading

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 6 Methods. Dr. Hikmat Jaber

Lecture 5: Methods CS2301

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

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Chapter 3 - Functions

Object Oriented Methods : Deeper Look Lecture Three

DUBLIN CITY UNIVERSITY

Computer Science & Engineering 150A Problem Solving Using Computers

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

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

Programming Language. Functions. Eng. Anis Nazer First Semester

1st and 3rd September 2015

Chapter 3 Function Basics

CS110: PROGRAMMING LANGUAGE I

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

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:

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

DUBLIN CITY UNIVERSITY

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Input and Expressions Chapter 3 Slides #4

CS110D: PROGRAMMING LANGUAGE I

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

Objectives. In this chapter, you will:

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Introduction to Programming

Lab Instructor : Jean Lai

CS2141 Software Development using C/C++ C++ Basics

(created by professor Marina Tanasyuk) FUNCTIONS

CS115 Principles of Computer Science

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CSCI 1061U Programming Workshop 2. Function Basics

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; }

Functions and Recursion

Absolute C++ Walter Savitch

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

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

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

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Chapter 5 Methods / Functions

C Programs: Simple Statements and Expressions

Introduction to C++ Introduction to C++ 1

Lecture #6-7 Methods

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

EECS402 Lecture 02. Functions. Function Prototype

Methods. CSE 114, Computer Science 1 Stony Brook University

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Polymorphism Part 1 1

Programming I Lecture 7

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

Chapter 6: Methods. Objectives 9/21/18. Opening Problem. Problem. Problem. Solution. CS1: Java Programming Colorado State University

JAVA Programming Concepts

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Chapter 2. Procedural Programming

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

A First Program - Greeting.cpp

Computing and Statistical Data Analysis Lecture 3

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Chapter 5 Methods. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Transcription:

Procedural Abstraction and Functions That Return a Value Savitch, 2007. Chapter 4 1

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

Top-Down Design Most programming languages provide for modular design: modules subroutines procedures functions These allow the programmer to break a complex task into smaller, more manageable modules: top-down design stepwise refinement divide and conquer iterative functional decomposition. 3

Example: The Mileage Program Mileage Program Display Compute Instructions Mileage Get Input Calculate Display Results Ask Again? Display Instructions Display Instructions Compute Mileage Get Input Calculate Display Results Again? 4

Predefined Functions C++ offers libraries of predefined functions. A header file must be included. Each has a single return type. Many require a list of one or more arguments. They are invoked (called) when encountered in expressions. The return value is used to evaluate the expression. 5

Example: The Square Root Function double sqrt(double number) input number The prototype delta = MAX_DOUBLE epsilon = MIN_DOUBLE quotient = number/2 return type name argument type parameter double sqrt(double number); delta > epsilon? new = number/quotient delta = new - quotient quotient = (new + quotient)/2 return quotient The invocation r = sqrt(n) ; 6

Some Math Functions Description Prototype Header File square root double sqrt(double n); cmath power double pow(double b, double e); cmath absolute value for int int abs(int n); cstdlib absolute value for long long labs(long n); cstdlib absolute value for double double fabs(double n); cmath ceiling double ceil(double n); cmath floor double floor(double n); cmath 7

A Function Call (Invocation) Header File Invocation 8

Type Casting To avoid truncation in division involving integers, force the literal to type double. Trouble occurs when neither operand is a literal. The solution lies in the type cast operator (C/C++). C++ provides the static cast: C++ also provides a functional notation: Be careful: 9

Programmer-Defined Functions referencing declaration (prototype) invocation defining declaration return to invocation argument to parameter 10

Declarations Declarations give meaning to identifiers. Defining declarations create storage for either data or algorithms. Referencing declarations refer to identifiers defined elsewhere. Prototypes are referencing declarations for functions (algorithms). 11

Arguments and Parameters An argument is a value of an expression passed to a function when it is invoked. Sometimes called an actual parameter. A parameter is a special local variable that is automatically initialized to the value of the argument passed to the function. Sometimes called a formal parameter. If an argument s type does not match that of the parameter, an automatic type conversion will occur if possible. Otherwise, a compiler error will result. 12

Golbal Constants //Computes the area of a circle and the volume of a sphere. //Uses the same radius for both calculations. #include <iostream> #include <cmath> const double PI = 3.14159; double area(double radius); //Returns the area of a circle with the specified radius. double volume(double radius); //Returns the volume of a sphere with the specified radius. 13

Local Namespaces int main( ) { using namespace std; double radius_of_both, area_of_circle, volume_of_sphere; } cout << "Enter a radius to use for both a circle\n" << "and a sphere (in inches): "; cin >> radius_of_both; area_of_circle = area(radius_of_both); volume_of_sphere = volume(radius_of_both); cout << "Radius = " << radius_of_both << " inches\n" << "Area of circle = " << area_of_circle << " square inches\n" << "Volume of sphere = " << volume_of_sphere << " cubic inches\n"; return 0; double area(double radius) { using namespace std; return (PI * pow(radius, 2)); } double volume(double radius) { using namespace std; return ((4.0 / 3.0) * PI * pow(radius, 3)); } 14

Procedural Abstraction The black box analogy input(s), if any results output(s), if any side effects, if any processing: iterative functional decomposition data algorithms Translation into a formal language Testing and debugging 15

Nuance One may deduce meaning from context Wash the shirt. Wash the car. Wash the dog. In everyday language, it s not necessary to say shirtwash the myshirt. carwash the Ford. dogwash Fido. 16

Name Overloading In C++ identifiers (names) may be loaded up with multiple meanings. The ambiguity is always resolved through context. The signature of a function is its name (identfier) plus the list of its parameter types. Function names may be overloaded (redefined) as long as the signatures differ. Differences in return type alone are not sufficient to constitute different signatures. Arguments are subject to automatic promotion and forced demotion. 17

Overloading Example 18

19