Using Free Functions Chapter 3 Computing Fundamentals with C++

Slides:



Advertisements
Similar presentations
Chapter Five Functions
Advertisements

Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Chapter 6: User-Defined Functions I
Overview creating your own functions calling your own functions.
CS 117 Spring 2002 Functions Hanly - Chapter 5 Friedman-Koffman - Sections & Chapter 6.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Introduction to Java Programming, 4E Y. Daniel Liang.
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Chapter 6: User-Defined Functions I
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
Data Types, Expressions and Functions (part I)
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
COMP102 Lab 091 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Functions Why we use functions C library functions Creating our own functions.
Chapter 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
CSC Programming for Science Lecture 7: Math Functions.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
3-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
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.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter Topics The Basics of a C++ Program Data Types
Chapter 6: User-Defined Functions I
Bill Tucker Austin Community College COSC 1315
Basic Elements of C++.
Methods Chapter 4: Methods Asserting Java ©Rick Mercer.
Chapter 3 Methods.
User-Defined Functions
Copyright © 2012 Pearson Education, Inc.
Basic Elements of C++ Chapter 2.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Announcements General rules about homeworks
Chapter 5 Function Basics
Announcements General rules about homeworks
The Language Package.
Functions October 23, 2017.
Functions.
Chapter 4 Implementing Free Functions
Chapter 5 Methods.
Announcements Homework 1 will be assigned this week,
Elementary Programming (C++)
Chapter 6: User-Defined Functions I
Announcements General rules about homeworks
Single-Result Functions & Modularity
Chapter 4 Methods Introducing Methods Declaring Methods
Presentation transcript:

Using Free Functions Chapter 3 Computing Fundamentals with C++ 3rd Edition Computing Fundamentals with C++ Rick Mercer Franklin, Beedle & Associates

Goals Evaluate some mathematical and trigonometric functions Use arguments in function calls Appreciate why programmers divide software into functions Read function headings so you can use existing functions

cmath functions C++ defines a large collection of standard math and trig functions such as sqrt(x) // return the square root of x fabs(x) // return the absolute value of x Functions are called by specifying the function name followed by the argument(s) in parentheses: cout << sqrt(4.0) << " " << fabs(-2.34); To use mathematical functions, you must #include <cmath> // For ceil, floor, pow

The pow function The pow function returns the first argument to the second argument's power pow(2.0, 3.0) returns 2 to the 3rd power (23 = 8.0) #include <cmath> // For the cmath functions #include <iostream> using namespace std; int main() { double base, power; base = 2.0; power = 4.0; cout << pow(base, power) << endl; cout << pow(-2, 3) << endl; return 0; }

Function Headings Can understand how to use a function if you see the function heading General form type functionName (type arg1, type arg2, … ) One function heading double pow(double base, double power) Comments example function calls also help pow(5.0, 3.0) // evaluates to 125

Some cmath functions double ceil(double x) Smallest integer >= x ceil(2.1) 3.0 double cos(double x) Cosine of x radians cos(1.0) 0.5403 double fabs(double x) Absolute value of x fabs(-1.5) 1.5 double floor(double x) Largest integer <= x floor(2.9) 2.0 double round(double x) Nearest integer to x round(1.5) 2 double sin(double x) Sine of x radians sin(1.0) 0.84147 double sqrt(double x) Square root of x sqrt(4.0) 2.0

Evaluate some Function Calls Different arguments cause different return values ceil(0.1)_____ sqrt(16.0)_____ ceil(1.1)_____ sqrt(sqrt(16))_____ pow(2.0, 3)_____ fabs(-1.2)_____ sqrt(4.0)_____ floor(3.99)_____

Rounding to n decimals Code that rounds x to n decimal places using the pow and floor functions x = 456.789; n = 2; x = x * pow(10, n); // x ________ x = x + 0.5; // x ________ x = floor(x); // x ________ x = x / pow(10, n); // x ________

Calling Documented Functions C++ has many free functions available Programmers can write their own free functions to develop programs Functions are more easily understood when documented with comment Functions may also include pre- and post- conditions next slide

Preconditions and Postconditions C++ comments that represents a contract between the implementers of a function and the user (client) of that function Precondition What the function requires to be true when called Postcondition What the function will do if the precondition(s) is/are met

Pre: and Post: conditions The preconditions are the circumstances that must be true in order for the function to successfully fulfill the postconditions Example Precondition abbreviates to pre: double sqrt(double x) // pre: x >= 0 // post: Returns square root of x sqrt(-1.0) evaluates to nan (not a number)

Function Headings A function heading is the complete declaration of a function without its implementation (sometimes called the function's signature). For example, this signature tells us how to call the function, but not how it works: double sqrt(double x)

Function Headings General form of a function heading: return-type function-name ( parameter-list ) return-type is any C++ type e.g. double int string function-name is any valid C++ identifier that is not reserved parameter-list is a set of 0 or more parameters General form for declaring parameters: class-name identifier Examples double f(double x) int max(int j, int k) string duplicate(string str, int n)

Argument/Parameter Associations Example call to max shows that arguments match parameters by position double max(double x, double y) cout << max(3.0, -5.32); The value of the 1st argument is copied to the 1st parameter, the value of the 2nd argument to the 2nd parameter, and so on, like assignments: x = 3.0; y = -5.32;

char and bool types C++ has two other primitive types that are used as the return types or parameters: char and bool bool type functions return 0 if the function returns false or a non-zero for true (usually 1) #include <cctype> // isdigit isalpha #include <iostream> using namespace std; int main() { cout << isdigit('9') << endl; // 1 cout << isdigit('X') << endl; // 0 cout << isalpha('A') << endl; // 1 cout << isalpha('<') << endl; // 0 return 0; }

char and int The toupper and tolower functions convert a char to its lower case or upper case equivalent Because the return type for both is int instead of char, the functions need to be cast to char with the code (char) to see the character toupper('a') // 65 (char) toupper('a') // A tolower('A') // 97 (char) tolower('A') // a

Summary Documented function headings provide the following information: The type of value returned by the function The function name The number of arguments to use in a call The type of arguments required in the function call Pre- and post-conditions tell us what the function will do if the preconditions are met