1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.

Slides:



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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Copyright © 2002 Pearson Education, Inc. Slide 1.
User Defined Functions
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 10 – Enhancing the Wage Calculator Application:
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
Functions Modules in C++ are called functions and classes
Chapter 7 Functions.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
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.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
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.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
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.
Learners Support Publications Functions in C++
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
1 CS161 Introduction to Computer Science Topic #9.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
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.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CHAPTER 6 USER-DEFINED FUNCTIONS I
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
CSCI 161: Introduction to Programming Function
User-Defined Functions
User Defined Functions
Lab 1 Introduction to C++.
FUNCTION CSC128.
Chapter 6: User-Defined Functions I
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006

2

3 Predefined Functions To use these functions you need to: 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

4 Predefined Functions : Value-Returning Functions Because the value returned by a value- returning function is unique, we must: Save the value for further calculation Use the value in some calculation Print the value A value-returning function is used in an assignment or in an output statement

5 Use of standard function pow #include using namespace std; int main() { double u,v; double result; u = 4.2; v = 3.0; return 0; } #include result=pow(u, v); cout << u << " to the power of " << v << " = " << pow(u, v) << endl; cout << "u = " << u << endl; u = u + pow(3, 3);

6 User-Defined Functions Void functions: do not have a data type Value-returning functions: have a data type

7 #include using namespace std; int main() { int i, j; int k; cin>>i>>j; if (i > j) { k = i; } else { k = j; } cout << "The max is " << k << endl; return 0; } #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); void PrintMax(int someNumber); int main() { int i, j; int k; cin>>i>>j; k = FindMax(i,j); PrintMax(k); // Prints Max Value return 0; } /* Function Definitions */ int FindMax(int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; } /* Function Definitions */ void PrintMax(int someNumber) { cout << "The max is " << someNumber << endl; } Value-returning functions Void functions

8 Value-Returning Functions Because the value returned by a value- returning function is unique, we must: Save the value for further calculation Use the value in some calculation Print the value A value-returning function is used in an assignment or in an output statement

9 #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); int main() { int i, j; int k; cin>>i>>j; k = FindMax(i,j); return 0; } /* Function Definitions */ int FindMax(int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; } cout << "The max is "<< FindMax(i,j) << endl; k =FindMax(i, j) + 1;

10 Value-Returning Functions: function definition Properties that form the function definition: Heading: 1. Name of the function 2. Number of parameters 3. Data type of each parameter 4. Function Type (type of the value returned by the function) Body: 1. Code required to accomplish the task (the body of the function) The syntax of the function definition is: functionType functionName(formal parameter list) { statements } The syntax of the formal parameter list is: dataType identifier, dataType identifier,...

11 Value-Returning Functions: function definition int FindMax(int n1, int n2) { if (n1 > n2) { return n1; } else { return n2; } Function Heading int FindMax(int n1, int n2) Function Body Function Type Function Name Formal parameter list

12 Value-Returning Functions: call a value-returning function To call a value-returning function: Use its name, with the actual parameters (if any) in parentheses 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,...

13 #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); int main() { int i, j; int k; cin>>i>>j; k = FindMax( i, j ) ; return 0; } /* Function Definitions */ int FindMax( int n1, int n2 ) { if (n1 > n2) { return n1; } else { return n2; } Actual parameter list Function Name Call a Value-returning Function

14 Function Prototype Function Prototype: function heading without the body of the function The syntax is: functionType functionName(parameter list); It is not necessary to specify the variable name in the parameter list The data type of each parameter must be specified Function prototypes appear before any function definition The compiler translates these first The compiler can then correctly translate a function call

15 #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); int main() { int i, j; int k; cin>>i>>j; k = FindMax( i, j ) ; return 0; } /* Function Definitions */ int FindMax( int n1, int n2 ) { if (n1 > n2) { return n1; } else { return n2; } Function Prototype

16 Value-Returning Functions (continued) Formal Parameter: variable declared in the heading Actual Parameter: variable or expression listed in a call to a function There is a one-to-one correspondence between actual and formal parameters

17 #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); int main() { int i, j; int k; cin>>i>>j; k = FindMax( i, j ) ; return 0; } /* Function Definitions */ int FindMax( int n1, int n2 ) { if (n1 > n2) { return n1; } else { return n2; } Actual parameter list Formal parameter list

18 Functions The formal parameter list can be empty If the formal parameter list is empty Parentheses are still needed Function heading of the value-returning function takes either of the following forms: functionType functionName() functionType functionName(void) In a function call the actual parameter is empty A call to a value-returning function with an empty formal parameter list is: functionName()

19 Value-Returning Functions : The return Statement Once the function computes the value, the function returns the value via the return statement The syntax of the return statement is: return expression or variable; 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

20 Flow of Execution Execution always begins at The first statement in the function main no matter where main is placed in the program Other functions are executed only when they are called A function call statement results in 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 passed back to the point immediately following the function call A value-returning function returns a value After executing the function The value that the function returns replaces the function call statement

21 #include using namespace std; /* Function Declarations */ int FindMax(int n1, int n2); int main() { int i, j; int k; cin>>i>>j; k = FindMax( i, j ) ; return 0; } /* Function Definitions */ int FindMax( int n1, int n2 ) { if (n1 > n2) { return n1; } else { return n2; }

22 End of lecture 16 Thank you!