C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
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.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
1 Lecture 3 Part 1 Functions with math and randomness.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 10 Functions – Part II
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
C++ Programming Lecture 12 Functions – Part IV
Programming Fundamentals Enumerations and Functions.
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.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
Dr. Shady Yehia Elmashad
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Dr. Shady Yehia Elmashad
CSCI 161: Introduction to Programming Function
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Chapter 6 - Functions Outline 5.1 Introduction
Chapter 6: User-Defined Functions I
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

The Hashemite University2 Outline Introduction. C++ program components. Math library functions. Functions: Prototype. Definition. Function call. Header files.

The Hashemite University3 Introduction Till now we have learned the basic concepts of C++. All the programs that we have written are simple, have few lines of code, and flat (i.e. all the code resides inside the main() body). But: What about large programs? What about code reuse (when the same code block is used many times within the program)? Divide and conquer technique: Construct a program from smaller pieces or components Each piece more manageable than the original program

The Hashemite University4 Program Components in C++ I C++ modules are functions and classes. Programs are written by: Combining new functions with “prepackaged” functions in the C++ standard library. The standard library provides a rich collection of functions. Functions are invoked by a function call A function call specifies the function name and provides information (as arguments) that the called function needs. Similar to the boss to worker analogy: A boss (the calling function or caller) asks a worker (the called function) to perform a task and return (i.e., report back) the results when the task is done.

The Hashemite University5 Program Components in C++ II Function definition: It is the body of the function which include all the processing done by the function. It makes use of the parameters (arguments) passed by the function call and specifies what will be returned by the function (result of the function). Only written once. These statements are hidden from other functions. Boss to worker analogy: The boss does not know how the worker gets the job done; he just wants it done

The Hashemite University6 Math Library Functions I Math library functions Allow the programmer to perform common mathematical calculations Are used by including the header file or Functions called by writing functionName (argument) Example cout << sqrt( ); Calls the sqrt (square root) function. The preceding statement would print 30 The sqrt function takes an argument of type double and returns a result of type double, as do all functions in the math library

The Hashemite University7 Math Library Functions II All math library functions return double values (as a result). Function arguments can be Constants sqrt( 4 ); Variables sqrt( x ); Expressions sqrt( sqrt( x ) ) ; sqrt( 3 - 6x );

The Hashemite University8 Math Library Functions III acos(x) inverse cosine, -1 <= x <= +1, returns value in radians in range 0 to PI asin(x) inverse sine, -1 <= x <= +1, returns value in radians in range 0 to PI atan(x) inverse tangent, returns value in radians in range -PI/2 to PI/2 cos(x) returns cosine of x, x in radians sin(x) returns sine of x, x in radians tan(x) returns tangent of x, x in radians exp(x) exponential function, e to power x log(x) natural log of x (base e), x > 0 sqrt(x) square root of x, x >= 0 fabs(x) absolute value of x floor(x) largest integer not greater than x ceil(x) smallest integer not less than x. pow(x, y) returns x y. fmod(x, y) computes the modulus of floating point numbers. Note: Have a look at Figure 3.2 in chapter 3.

The Hashemite University9 Functions Allow the programmer to modularize a program. Local variables Known only in the function in which they are defined. All variables declared in function definitions are local variables. Parameters Local variables passed when the function is called that provide the function with outside information.

The Hashemite University10 1// Fig. 3.3: fig03_03.cpp 2// Creating and using a programmer-defined function 3#include 4 5using std::cout; 6using std::endl; 7 8int square( int ); // function prototype 9 10int main() 11{ 12 for ( int x = 1; x <= 10; x++ ) 13 cout << square( x ) << " "; cout << endl; 16 return 0; 17} 18 19// Function definition 20int square( int y ) 21{ 22 return y * y; 23} Notice how parameters and return value are declared.

The Hashemite University11 Function Definitions Create customized functions to: Take in data Perform operations Return the result Format for function definition: return-value-type function-name ( parameter-list ) { declarations and statements } Function name is any valid identifier. Example : int square( int y) { return y * y; }

The Hashemite University12 Function Prototypes I Used by the compiler to check the validity of the function call within the main program (function name, its return data type, number of arguments, their data types, and their order). Function prototype consist of: Function name Parameters Information the function takes in, can be void. Return type Type of information the function passes back to caller (default int ) void signifies the function returns nothing

The Hashemite University13 Function Prototypes II If you have passed arguments of different data types from the one specified in the function prototype and definition, the compiler will convert them to the proper data type for the function (implicit casting). However, sometimes implicit casting is not possible. In this case the compiler will give a syntax error. Not following the Promotion Rule in C++ an cause errors in the obtained results. E.g. converting from double to integer causes data loss but converting integer to double is not. Function prototype is Only needed if function definition comes after the function call in the program (after the main()). Another example: int maximum( int, int, int ); Takes in 3 int s Returns an int

The Hashemite University14 Functions Returning Results Three ways to return to the point at which we have called a function: Reaching closing brace } of the function definition (the function return nothing).. Executing return; (the function returns nothing). Executing return result; (the function returns a specific result).

The Hashemite University15 1// Fig. 3.4: fig03_04.cpp 2// Finding the maximum of three integers 3#include 4 5using std::cout; 6using std::cin; 7using std::endl; 8 9int maximum( int, int, int ); // function prototype 10 11int main() 12{ 13 int a, b, c; cout << "Enter three integers: "; 16 cin >> a >> b >> c; // a, b and c below are arguments to 19 // the maximum function call 20 cout << "Maximum is: " << maximum( a, b, c ) << endl;

The Hashemite University return 0; 23} 24 25// Function maximum definition 26// x, y and z below are parameters to 27// the maximum function definition 28int maximum( int x, int y, int z ) 29{ 30 int max = x; if ( y > max ) 33 max = y; if ( z > max ) 36 max = z; return max; 39} Enter three integers: Maximum is: 85 Enter three integers: Maximum is: 92 Enter three integers: Maximum is: 98

The Hashemite University17 Header Files Two types: Standard (prepackaged) Header files Contain function prototypes for library functions,, etc. Load with #include Example: #include Custom header files Defined by the programmer Save as filename.h Loaded into program using #include "filename.h“ Have a look at Figure 3.6 in chapter 3.

The Hashemite University18 Additional Notes This lecture covers the following material from the textbook: Chapter 3: Sections 3.1 – 3.7