Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.

Slides:



Advertisements
Similar presentations
Methods Java 5.1 A quick overview of methods
Advertisements

Chapter Five Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers.
Introduction to Computers and Programming Introduction to Methods in Java.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Function 2 (L17) * Function Prototype * Promotion Rules * Data Type * Library Header File * Customer Header File * Case Study * Exercise/Home Work Dr.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
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 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
1 Lecture 3 Part 1 Functions with math and randomness.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
Review 2 - Chapter 3 and 4 Function 1 Function 2 Function 3 Function 4
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Functions Why we use functions C library functions Creating our own functions.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Functions Review.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Function ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) Fax: (757) Office: Gosnold 217b Subject Overview.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Functions Venkatesh Ramamoorthy 21-March Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Dr. Shady Yehia Elmashad
IS Program Design and Software Tools Introduction to C++ Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
CSCI 161: Introduction to Programming Function
Functions.
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Using Free Functions Chapter 3 Computing Fundamentals with C++
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function Definition * Examples for calling Functions * Common Programming Errors - Exercise/How Work Dr. Ming Zhang

Mathematical Library Functions Function Name & Argument(s) Description int abs(int i) Absolute value of i double fabs(double d) Absolute value of d double pow(double d1, double d2) d1 raised to d2 power double exp(double d) e raised to d power double sqrt(double d) square root of d double sin(double d) Sine of d(d in radians) double cos(double d) Cosine of d(d: radians) double log(double d) Natural log of d double log10(double d) Common log of d Completing the Basic Dr. Ming Zhang

Mathematical Library Functions Examples Example Returned Value abs( -3) 3 fabs(-7.362) pow(2.0, 5.0) exp(-3.2) sqrt(16.0) sin(0.0) cos(0.0) log(18.697) log10(18.697) Completing the Basic Dr. Ming Zhang

Program Components in C++ *Models in C++ - Functions - Classes * Functions - Pre-packaged function C++ standard library functions - Programmer-defined functions The programmer writes Dr. Ming Zhang

Function Call * Function Call The function call specifies the function name and provides information (as arguments) that the called function needs to do its job. And the called function returns the result to calling function (caller) * Example …… double time, height; height = 800.0; time = sqrt( 2.0 * height/32.2 ); …… Dr. Ming Zhang

Function Relationship main( ) function1 function2 function3 function11 function12 function31 function111 function112 function311 Dr. Ming Zhang

Motivations of Functionalizing a Program * More Manageable The divide-and-conquer approach makes program development more manageable. * Software Reusability Using existing functions as building blocks to create new programs. * Avoiding Repeating Code in a Program Packaging code as a function allows the code to be executed from several locations in a program simple by calling the function. Dr. Ming Zhang

Function Prototype * Function Prototype return-value-type function-name( data-type list) * Example of Function Prototype int square (int ) * The function prototype is not required if the definition of the function appears before the function’s first use in the program. In such case, the function definition also acts as the function prototype. Dr. Ming Zhang

Format of Function Definition * Format of Function Definition return-value-type function-name (parameter-list) { declarations statements } * Example int square (int y) { return y*y; } Dr. Ming Zhang

Three Returns of a Function * Control is returned simple when the function-ending right brace is reached. Function does not return a result. * Executing the statement return; Function does not return a result. * If function does return a result, the statement return expression; returns the value of expression to the caller. Dr. Ming Zhang

Example of Using Function (Fig. 3.3) # include using std::cout; int square ( int); // function prototype int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y) // Function definition { return y*y; } Dr. Ming Zhang

Example of Using Function (Fig. 3.4) …… int maximum ( int, int, int); //function prototype ….. cout << maximum (a, b, c); // call function …… int maximum (int x, int y, int z) { int max =x; if (y> max) max =y; if (z > max) max = z; return max } Dr. Ming Zhang

Common Error 1 -Exercise/Home work # include using std::cout; int square ( int); int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } void square( int y) { return y*y; } ERROR Dr. Ming Zhang

Common Error 2 -Exercise/Home work int maximum ( int, int, int); //function prototype ….. cout << maximum (a, b, c); // calling function …… int maximum (int x, y, z) { int max =x; if (y> max) max =y; if (z > max) max = z; return max } ERROR Dr. Ming Zhang

Common Error 3 -Exercise/Home work # include using std::cout; int square ( int); int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y); { return y*y; } ERROR Dr. Ming Zhang

Common Error 4 -Exercise/Home work # include using std::cout; int square ( int); int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y) { int y; return y*y; } ERROR Dr. Ming Zhang

Common Error 5 -Exercise/Home work # include using std::cout; void printName( ); int main ( ) { for ( int x =1; x <=10; x++) cout << printName<<endl; return(0); } int printName( ) { cout << “Ming Zhang” << endl; return; } ERROR Dr. Ming Zhang

Common Error 6 -Exercise/Home work # include using std::cout; using std::endl; int square ( int); void printName( ); int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y) { void printName( ) { cout << “Ming Zhang”<< endl;} return y*y; } ERROR Dr. Ming Zhang

Common Error 7 -Exercise/Home work # include using std::cout; int square ( float); int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y) { return y*y; } ERROR Dr. Ming Zhang

Common Error 8 -Exercise/Home work # include using std::cout; int square ( int) int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y) { return y*y; } ERROR Dr. Ming Zhang