© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

User Defined Functions
Spring Semester 2013 Lecture 5
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Chapter Five Functions
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 3 Function Basics Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Functions Call by reference.
Chapter 5 Functions.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CSC 200 Lecture 04 2/8/06 Matt Kayala. Learning Objectives  Predefined Functions  Those that return a value and those that don’t  Programmer-defined.
Basic Elements of C++ Chapter 2.
COMPUTER SCIENCE I C++ INTRODUCTION
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CPS120: Introduction to Computer Science Decision Making in Programs.
Slide 1 Chapter 3 Function Basics. Slide 2 Learning Objectives  Predefined Functions  Those that return a value and those that don’t  Programmer-defined.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
1 CS161 Introduction to Computer Science Topic #10.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
CPS120: Introduction to Computer Science Lecture 14 Functions.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Arrays.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
A Sample Program #include using namespace std; int main(void) { cout
Operator Overloading Introduction
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
Chapter 7: User-Defined Functions II
Introduction to C++ computers and programming
Basic Elements of C++.
CSCI 161: Introduction to Programming Function
Basic Elements of C++ Chapter 2.
FIGURE 4-10 Function Return Statements
User Defined Functions
FIGURE 4-10 Function Return Statements
CS150 Introduction to Computer Science 1
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
FIGURE 4-10 Function Return Statements
Presentation transcript:

© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)

© Janice Regan, CMPT 128, steps: User defined functions  Function Declaration/prototype  Information than the compiler needs to properly interpret calls to the function  Function Definition  Actual implementation of the function  Function Call  Using the function in the calling function

© Janice Regan, CMPT 128, Declaring a user’s function  A function prototype tells us (and the compiler)  the name and type of the function  The identifier of the function  The type of each parameter  The identifier of each parameter (optional)  The order of the parameters  The function prototype is usually placed  after the #include statements  before the int main () statement  The function can then be called by any other functions

© Janice Regan, CMPT 128, Writing a function prototype  The function declaration or function prototype always has a similar form. ( ); int myMean ( int a, int b, int c, int d); double sumOfSquares ( double in1, double in2); bool isReady ( int, double, int ); // In a prototype identifiers in the parameter list are optional

© Janice Regan, CMPT 128, Understanding the prototype (1)  The function declaration or function prototype always has a similar form. ( );  The function identifier indicates the name of the function  The return type indicates the type of the value returned by the function  When the function is called the statement calling the function can be considered to be an expression  The value of this expression (the call) in the calling function has a type, return type

© Janice Regan, CMPT 128, Understanding the prototype (2)  The function declaration or function prototype always has a similar form. ( );  A series of parameters (or arguments) follow the function identifier.  These parameters indicate the data that is supplied to (and sometimes under special conditions returned from) the function  Each parameter is of a particular data type that is specified within the declaration

© Janice Regan, CMPT 128, Understanding the prototype (3)  The function declaration or function prototype always has a similar form. ( );  each parameter in the parameter list includes a type and an optional identifier  The function can have any number of parameters  Each parameter of the function can have any type  Different parameters can have different types  Order of the parameters is important Parameters in the declaration, definition, and call must always be in the same order

© Janice Regan, CMPT 128, Prototypes: an example  As an example consider double sinc ( double x ); OR double sinc ( double );  The identifier (name) of the function is sinc  The function returns a value of type double to the calling function The value of the function call, the expression sinc(inputvalue) in the calling function is of type double  The parameter list contains a single parameter of type double An identfier for the parameter may be given (optional)

© Janice Regan, CMPT 128, steps: User defined functions  Function Declaration/prototype  Information than the compiler needs to properly interpret calls to the function  Function Definition  Actual implementation of the function  Function Call  Using the function in the calling function

© Janice Regan, CMPT 128, Function Definition  Two main parts  Function Head Gives the compiler information to match the function with the function declaration  Function Body Actual C++ statements implementing the function  Function definitions are placed after or before the main function (not inside the main function)

© Janice Regan, CMPT 128, Placement of Function Definition Int main () { // body of main function } double sinc(double x) // function head { //function body }

© Janice Regan, CMPT 128, Sample Function Definition double sinc(double x) { if (fabs(x) < ) { return(1.0); } else { return( sin(x)/x); } Function head Function body Function call to library function sin

© Janice Regan, CMPT 128, Function Head (examples)  T he first line of a function definition is the function head  The head for the main function is int main ( )  The head for our example function is double sinc (double x)  There is no ; at the end of a function head

 The function head indicates the type of the function  double sinc( double x )  The function head indicates a function name or identifier  double sinc( double x )  The function head indicates a list of parameters for the function, each parameter includes a type and an identifier double sinc (double x) © Janice Regan, CMPT 128, Function Head (content)

© Janice Regan, CMPT 128, Function head: formal parameters  The parameters in the function head are referred to as formal parameters  The function has 0 or more formal parameters double sinc( double x )  Each of a function’s formal parameters have types double sinc( double x )  A function may have formal parameters of more than one type Each parameter must be given its own type Multiple parameters are separated by commas int sample( double x, int y, char z, double a)

© Janice Regan, CMPT 128, The body of a function  After the function head the body of the function is enclosed in {} double sinc(double x) { //variable declarations double y; //declaring variable //for return value //calculations to determine y return(y); } Function body

© Janice Regan, CMPT 128, Parts of the function body  Local variables are declared  Calculate the return value of the function  Return the value to the calling function  return (returnValue);  In a more complicated function you must assure all possible paths to completion of a function end with a return statement  For a void function return statements are not required  Return statements without arguments may be used to return from the function to the calling program

© Janice Regan, CMPT 128, steps: User defined functions  Function Declaration/prototype  Information than the compiler needs to properly interpret calls to the function  Function Definition  Actual implementation of the function  Function Call  Using the function in the calling function

© Janice Regan, CMPT 128, steps: User defined functions  Function Declaration/prototype  Information than the compiler needs to properly interpret calls to the function  Function Definition  Actual implementation of the function  Function Call  Using the function in the calling function

© Janice Regan, CMPT 128, Calling a function float fabs(float x); … limitValue = fabs(-9.7);  fabs(-9.7) is an expression known as a function call, or function invocation  The arguments in the brackets ( ) of a function call are called actual arguments or actual parameters.  An actual argument in a function call can be A literal (like -9.7) any variable whose value is of the correct type any expression whose value is of the correct type

© Janice Regan, CMPT 128, Calling a function Consider a void function (returns no value)  A function call to a void function does not have a value (a void function does not return a value)  A function call to a void function cannot be used in an expression Consider calling a function that is not void  A function call to a non-void function has a value so it can be used as part of a more complicated expressions  bonus = fabs(mylimit) * myfactor;  A function call to a function of any non void type is allowed wherever it’s legal to use an expression

© Janice Regan, CMPT 128, Returning a function’s value (1)  A non-void function will take the supplied values of the actual parameters, calculate a result, then return that result to the calling program  The function has a type. The type of the function is the type of the value returned by that function to the calling program  A function is invoked using a function call, the function call expression is given the value that is returned by the function

© Janice Regan, CMPT 128, Sample Function Definition double sinc(double x) { if (fabs(x) < ) { return(1.0); } else { return( sin(x)/x); } Function head Function body Function call to library function sin

© Janice Regan, CMPT 128, Using our sample function // declare functions used double sinc(double x); int main (void) { // declare variables double a, b; // obtain input data a, call function, print results cout << “ enter value for which sinc is to be determined “); cin >> a; b= sinc(a); cout << "sinc( " << a << " ) = " << b; } Function prototype or function declaration Function call

© Janice Regan, CMPT 128, Returning a function’s value (2)  Our sample function determines the value of sin(x)/x when we supply a value for the parameter x  The function sinc(x) will take the value of x, calculate the value of sin(x)/x and return the resulting value to the calling program  To return the value of the function to the calling program following command is used return(ValueToBeReturned); The type of variable or expression ValueToBeReturned should match the type of the function returning the value  A function of any type other than void must contain at least one return statement. It may contain more. There must be 1 return statement ending each flow of control through the function

© Janice Regan, CMPT 128, Calling Void Functions  For a function declared as void showResults(double x, double y);  Can call the function in a calling function as follows  showResults(degreesF, degreesC);  showResults(32.5, 0.3);  A function call to a void function does not have a value, because a void function does not return a value.  A = showResults(32.5,0.3) * 3.0; //this is an invalid statement  A function call to a void function has no value to be used in an arithmetic expression ( or any other kind of expression)  A function call to a void function cannot be assigned to a variable since it has no value to place in that variable