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

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

Functions. Lecture 6 COP 3014 Spring February 11, 2018


Programming Language. Functions. Eng. Anis Nazer First Semester

C++ Programming Lecture 11 Functions Part I

Chapter 6 - Notes User-Defined Functions I

Introduction to Programming

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

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

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

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

C++ Programming: Functions

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

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Computer Science & Engineering 150A Problem Solving Using Computers

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Objectives. In this chapter, you will:

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

Chapter 7 User-Defined Methods. Chapter Objectives

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

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

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

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

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

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

Introduction. What is function? Multiple functions form a larger program Modular programming

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

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

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

Question 2. [5 points] Given the following symbolic constant definition

CS313D: ADVANCED PROGRAMMING LANGUAGE

C Functions. 5.2 Program Modules in C

Chapter 3 Function Basics

Using Free Functions

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

C Programming for Engineers Functions

Fundamentals of Programming & Procedural Programming

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

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

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

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

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

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

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

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

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Chapter 3 - Functions

CT 229 Java Syntax Continued

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

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

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

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

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

Computing and Statistical Data Analysis Lecture 3

The Math Class. Using various math class methods. Formatting the values.

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.

2. Functions I: Passing by Value

Department of Computer and Mathematical Sciences. Lab 10: Functions. CS 1410 Intro to Computer Science with C++

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

(created by professor Marina Tanasyuk) FUNCTIONS

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++

Introduction to Computer Science Unit 2. Notes

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

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

6.096 Introduction to C++

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Functions and Recursion

BITG 1113: Function (Part 2) LECTURE 5

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Lecture 18 Tao Wang 1

SUHAIL T A

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Object Oriented Methods : Deeper Look Lecture Three

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

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Probably the best way to start learning a programming language is with a program. So here is our first program:

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

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

Chapter 5 C Functions

Summary of basic C++-commands

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

6. User Defined Functions I

CS-201 Introduction to Programming with Java

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CSE 230 Intermediate Programming in C and C++ Functions

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

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

A SHORT COURSE ON C++

Transcription:

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

In this chapter, you will: Objectives Learn about standard (predefined) functions and discover how to use them in a program Learn about user-defined functions Examine value-returning functions, including actual and formal parameters Explore how to construct and use a valuereturning, user-defined function in a program C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2

Introduction Functions are like building blocks They allow complicated programs to be divided into manageable pieces Some advantages of functions: A programmer can focus on just that part of the program and construct it, debug it, and perfect it Different people can work on different functions simultaneously Can be re-used (even in different programs) Enhance program readability C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3

Introduction (cont'd.) Functions Called modules Like miniature programs Can be put together to form a larger program C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4

Predefined Functions In algebra, a function is defined as a rule or correspondence between values, called the function s arguments, and the unique value of the function associated with the arguments If f(x) = 2x + 5, then f(1) = 7, f(2) = 9, and f(3) = 11 1, 2, and 3 are arguments 7, 9, and 11 are the corresponding values C++ Programming: From Problem Analysis to Program Design, Fifth Edition 5

Predefined Functions (cont'd.) Some of the predefined mathematical functions are: sqrt(x) pow(x, y) floor(x) Predefined functions are organized into separate libraries I/O functions are in iostream header Math functions are in cmath header C++ Programming: From Problem Analysis to Program Design, Fifth Edition 6

Predefined Functions (cont'd.) pow(x,y) calculates x y pow(2, 3) = 8.0 Returns a value of type double x and y are the parameters (or arguments) The function has two parameters sqrt(x) calculates the nonnegative square root of x, for x >= 0.0 sqrt(2.25) is 1.5 Type double C++ Programming: From Problem Analysis to Program Design, Fifth Edition 7

Predefined Functions (cont'd.) The floor function floor(x) calculates largest whole number not greater than x floor(48.79) is 48.0 Type double Has only one parameter C++ Programming: From Problem Analysis to Program Design, Fifth Edition 8

Predefined Functions (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 9

Predefined Functions (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 10

Predefined Functions (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 11

Predefined Functions (cont'd.) Example 6-1 sample run: C++ Programming: From Problem Analysis to Program Design, Fifth Edition 12

User-Defined Functions Value-returning functions: have a return type Return a value of a specific data type using the return statement Void functions: do not have a return type Do not use a return statement to return a value C++ Programming: From Problem Analysis to Program Design, Fifth Edition 13

Value-Returning Functions To use these functions you must: Include the appropriate header file in your program using the include statement Know the following items: Name of the function Number of parameters, if any Data type of each parameter Data type of the value returned: called the type of the function C++ Programming: From Problem Analysis to Program Design, Fifth Edition 14

Value-Returning Functions (cont'd.) Because the value returned by a valuereturning function is unique, must: Save the value for further calculation Use the value in some calculation Print the value A value-returning function is used in an assignment or in an output statement One more thing is associated with functions: The code required to accomplish the task C++ Programming: From Problem Analysis to Program Design, Fifth Edition 15

Value-Returning Functions (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 16

Value-Returning Functions (cont'd.) Heading: first four properties above Example: int abs(int number) Formal Parameter: variable declared in the heading Example: number Actual Parameter: variable or expression listed in a call to a function Example: x = pow(u, v) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 17

Syntax: Syntax: Value-Returning Function functiontype is also called the data type or return type C++ Programming: From Problem Analysis to Program Design, Fifth Edition 18

Syntax: Formal Parameter List C++ Programming: From Problem Analysis to Program Design, Fifth Edition 19

Function Call C++ Programming: From Problem Analysis to Program Design, Fifth Edition 20

Syntax: Actual Parameter List The syntax of the actual parameter list is: Formal parameter list can be empty: A call to a value-returning function with an empty formal parameter list is: C++ Programming: From Problem Analysis to Program Design, Fifth Edition 21

return Statement Once a value-returning function computes the value, the function returns this value via the return statement It passes this value outside the function via the return statement C++ Programming: From Problem Analysis to Program Design, Fifth Edition 22

Syntax: return Statement The return statement has the following syntax: In C++, return is a reserved word When a return statement executes Function immediately terminates Control goes back to the caller When a return statement executes in the function main, the program terminates C++ Programming: From Problem Analysis to Program Design, Fifth Edition 23

Syntax: return Statement (cont d.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 24

Function Prototype Function prototype: function heading without the body of the function Syntax: It is not necessary to specify the variable name in the parameter list The data type of each parameter must be specified C++ Programming: From Problem Analysis to Program Design, Fifth Edition 25

Function Prototype (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 26

Function Prototype (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 27

Value-Returning Functions: Some Peculiarity C++ Programming: From Problem Analysis to Program Design, Fifth Edition 28

Value-Returning Functions: Some Peculiarity (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 29

Value-Returning Functions: Some Peculiarity (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 30

Example 6-5: Palindrome Number A nonnegative integer is a palindrome if it reads forward and backward in the same way Examples: 5, 44, 789656987 C++ Programming: From Problem Analysis to Program Design, Fifth Edition 31

Example 6-5: Palindrome Number (cont d.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 32

Flow of Execution Execution always begins at the first statement in the function main Other functions are executed only when they are called Function prototypes appear before any function definition The compiler translates these first The compiler can then correctly translate a function call C++ Programming: From Problem Analysis to Program Design, Fifth Edition 33

Flow of Execution (cont'd.) A function call results in transfer of control to the first statement in the body of the called function After the last statement of a function is executed, control is passed back to the point immediately following the function call A value-returning function returns a value After executing the function the returned value replaces the function call statement C++ Programming: From Problem Analysis to Program Design, Fifth Edition 34

Programming Example: Largest Number The function larger is used to determine the largest number from a set of numbers Program determines the largest number from a set of 10 numbers Input: a set of 10 numbers Output: the largest of 10 numbers C++ Programming: From Problem Analysis to Program Design, Fifth Edition 35

Programming Example: Program Analysis Suppose that the input data is: 15 20 7 8 28 21 43 12 35 3 Read the first number of the data set Because this is the only number read to this point, you may assume that it is the largest number so far and call it max Read the second number and call it num Compare max and num, and store the larger number into max C++ Programming: From Problem Analysis to Program Design, Fifth Edition 36

Programming Example: Program Analysis (cont'd.) Now max contains the larger of the first two numbers Read the third number and compare it with max and store the larger number into max max contains the largest of the first three numbers Read the next number, compare it with max, and store the larger into max Repeat this process for each remaining number in the data set C++ Programming: From Problem Analysis to Program Design, Fifth Edition 37

Programming Example: Algorithm Design Read the first number Because this is the only number that you have read, it is the largest number so far Save it in a variable called max For each remaining number in the list Read the next number Store it in a variable called num Compare num and max C++ Programming: From Problem Analysis to Program Design, Fifth Edition 38

Programming Example: Algorithm Design (cont'd.) For each remaining number in the list (cont'd.) If max < num num is the new largest number update the value of max by copying num into max If max >= num, discard num; that is, do nothing Because max now contains the largest number, print it C++ Programming: From Problem Analysis to Program Design, Fifth Edition 39

Summary Functions (modules) are miniature programs Divide a program into manageable tasks C++ provides the standard functions Two types of user-defined functions: valuereturning functions and void functions Variables defined in a function heading are called formal parameters Expressions, variables, or constant values in a function call are called actual parameters C++ Programming: From Problem Analysis to Program Design, Fifth Edition 40

Summary (cont'd.) In a function call, the number of actual parameters and their types must match with the formal parameters in the order given To call a function, use its name together with the actual parameter list Function heading and the body of the function are called the definition of the function A value-returning function returns its value via the return statement C++ Programming: From Problem Analysis to Program Design, Fifth Edition 41

Summary (cont'd.) A prototype is the function heading without the body of the function; prototypes end with the semicolon Prototypes are placed before every function definition, including main User-defined functions execute only when they are called In a call statement, specify only the actual parameters, not their data types C++ Programming: From Problem Analysis to Program Design, Fifth Edition 42