Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.

Slides:



Advertisements
Similar presentations
Chapter 6: User-Defined Functions I
Advertisements

User Defined Functions
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
CS 201 Functions Debzani Deb.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6: User-Defined Functions I
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Functions Modules in C++ are called functions and classes
Chapter 7 Functions.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
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.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
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.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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.
Scis.regis.edu ● CS-361: Control Structures Week 2 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn about.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
CHAPTER 6 USER-DEFINED FUNCTIONS I
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
Function Topic 4.
Chapter 6: User-Defined Functions I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
CSCI 161: Introduction to Programming Function
User-Defined Functions
Dr. Shady Yehia Elmashad
User Defined Functions
Chapter 5 Function Basics
FUNCTION CSC128.
Chapter 6: User-Defined Functions I
Functions Imran Rashid CTO at ManiWeber Technologies.
CPS125.
Presentation transcript:

Chapter 6 User-Defined Functions I

Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning functions Actual parameters Formal parameters Construction, and Usage

What is a Function? Building blocks of a program Manageable subdivision of a program Often called modules Like a miniature program Designed to perform a specific task

Advantages of Using Functions Allow complicated programs to be divided into manageable pieces A programmer can focus on a function and construct it, debug it, and perfect it A large program may be divided amongst more than one programmer to work on simultaneously Can be used in more than one place in the program or even in different programs

Types of Functions Void Functions Do not have a return type Value-Returning Functions Has a data type The data type of a function is determined by the type of data returned

Predefined Functions Organized into separate libraries Math functions are in the cmath header I/O functions are in the iostream header To use you must: Include the correct header file Know the name of the function Know the number of parameters, if any Know the data type of each parameter Know the data type of the value computed by the function, called the type of the function

Predefined Functions (continued) Some examples of predefined mathematical functions are: pow(x, y) sqrt(x) floor(x) abs(x) To see a full list type “cmath header” into the Visual Studio help index

The pow Function pow(x,y) calculates x y, pow(2,3) = 8.0 pow returns a value of type double x and y are called the parameters (or arguments) of the function pow The function pow has two parameters To determine the types of parameters the pow function requires see the help documentation.

The sqrt Function sqrt(x) calculates the non-negative square root of x for x >= 0.0 sqrt returns a value of type double The function sqrt has only one parameter, of type double

The floor Function floor(x) calculates the largest whole number not greater than x floor returns a value of type double The function floor has only one parameter, of type double

Value-Returning Functions The value returned by a function is unique A value returning function may be used in an assignment or an output statement To make using a function worthwhile we must do one or more of the following: Save the value returned by the function Use the value in some calculation Print the value

Value-Returning Functions Components of the function definition: Name the function Number and type of parameters Type of the function (return type) The code required to accomplish the task (the body of the function)

Value-Returning Functions (contd.) Heading: made of of the name of the function, number and type of parameters, type of the function Formal Parameter: variable declared in the function heading Actual Paramter: variable or expression listed in a call to a function

Value-Returning Function (contd.) Syntax: functionType FunctionName (formal parameter list) { statements } functionType : the type of the value returned by the function; also called the data type.

Syntax The syntax of the formal parameter list is: dataType identifier, dataType identifier The syntax for a function call is: FunctionName(actual parameter list) The syntax for the actual parameter list is: expression or variable, expression or variable

Functions The formal parameter list can be empty If the formal parameter list is empty The parenthesis are still needed Function heading of the value-returning function takes the following form functionType FunctionName ( ) In a function call the actual parameter list is empty and takes the form FunctionName( )

Value-Returning Functions To call a value-returning function: Use its name, with the actual parameters (if any) in parenthesis Remember that there is a one-to-one correspondence between actual parameters and formal parameters

Value-Returning Functions (contd.) A value-returning function is called in an expression Expression may be part of an assignment statement or an output statement A function call in a program results in the execution of the body of the called function

The return Statement Once the function computes the value, the function returns the value via the return statement. 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. The syntax of a return statement is: return expression;

Example double Larger ( double x, double y ) { if ( x >= y ) return x; return y; } Function type: double Formal Parameters: x and y

Function Prototype Function Prototype: function heading without the body of the function functionType FunctionName (parameter list); It is not necessary to specify the variable name in the parameter list of the function prototype The data type of each parameter must be specified

Example double Larger ( double x, double y ) { if ( x >= y ) return x; return y; } Function Prototype double Larger ( double x, double y ); or, double Larger ( double, double );

Flow of Execution Execution always begins in the function main no matter where main is placed in the program Other functions are executed only when they are called

Flow of Execution (continued) A function call statement results in the transfer of control to the first statement in the body of the called function After the last statement of the called function is executed, control is transferred back to the point immediately following the function call

Flow of Execution (continued) A value-returning function returns a value After executing the function, the value the function returns replaces the function call statement.

Putting everything together… #include using namespace std; // function prototypes double Larger ( double, double ); int main ( ) { double num1 = ; double num2 = ; double largest = 0.0; // function calls largest = Larger ( num1, num2 ); return 0; } // function definitions double Larger ( double x, double y ) { if ( x >= y ) return x; return y; }

Programming Example Write a function that returns the length of the hypotenuse of a right triangle given the lengths of the two legs a and b. To calculate the length of the hypotenuse use the pythagorean theorem a 2 + b 2 = c 2 where a and b are the lengths of the legs of the right triangle and c is the length of the hypotenuse.

Programming Example (contd.) c = The length of the hypotenuse is calculated by using the formula

Programming Example (contd.) Prototype: double CalculateHypotenuse ( double a, double b ); Definition: double CalculateHypotenuse ( double a, double b ) { double hypotenuse = 0.0; hypotenuse = sqrt(pow(a, 2.0) + pow(b, 2.0)); return hypotenuse; }

Using the CalculateHypotenuse Function in a Program #include using namespace std; double CalculateHypotenuse ( double, double ); int main ( ) { double sideA = 0.0; double sideB = 0.0; cout << “Please enter the lengths of the sides of the” << “right triangle, a and b: ” << endl; cin >> sideA >> sideB; cout << fixed << showpoint << setprecision(3) << endl << endl << “The length of the hypotenuse of the right triangle whose\nlegs are lengths ” << sideA << “ and ” << sideB << “ is ” << CalculateHypotenuse(sideA, sideB); return 0; } double CalculateHypotenuse ( double a, double b ) { double hypotenuse = 0.0; hypotenuse = sqrt ( pow ( a, 2.0 ) + pow ( b, 2.0 ) ); return hypotenuse; }

Lab Assignment Programming Exercise #6, Pg. 343

Homework Assignment Exercise #1, Pg. 337 Exercise #5, Pg. 338 Programming Exercise #5, Pg. 343