Lecture 16:User-Definded function I - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 16:User-Definded function I

Description:

Introduction to Computer Science. Spring 2006. 2. 3. Predefined Functions ... Function Definitions */ void PrintMax(int someNumber) ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 23
Provided by: xw
Category:

less

Transcript and Presenter's Notes

Title: Lecture 16:User-Definded function I


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

2
(No Transcript)
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 ltiostreamgt using namespace std int
main() double u,v double result u
4.2 v 3.0 return 0
cout ltlt u ltlt " to the power of " ltlt v ltlt " " ltlt
pow(u, v) ltlt endl
6
User-Defined Functions
  • Void functions do not have a data type
  • Value-returning functions have a data type

7
include ltiostreamgt using namespace std /
Function Declarations / int FindMax(int n1, int
n2) void PrintMax(int someNumber) int
main() int i, j int k
cingtgtigtgtj k FindMax(i,j)
PrintMax(k)     // Prints Max Value
return 0
include ltiostreamgt using namespace std int
main() int i, j int k
cingtgtigtgtj if (i gt j) k i
else k j
cout ltlt "The max is " ltlt k ltlt
endl return 0
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 ltiostreamgt using namespace std /
Function Declarations / int FindMax(int n1, int
n2) int main() int i, j int k
cingtgtigtgtj k FindMax(i,j)
return 0 / Function Definitions / int
FindMax(int n1, int n2) if (n1 gt n2)
return n1 else
return n2
cout ltlt "The max is "ltlt FindMax(i,j) ltlt endl
k FindMax(i, j) 1
10
Value-Returning Functions function definition
  • Properties that form the function definition
  • Heading
  • Name of the function
  • Number of parameters
  • Data type of each parameter
  • Function Type (type of the value returned by the
    function)
  • Body
  • 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
Function Type
Function Name
Formal parameter list
Function Heading
int FindMax(int n1, int n2)
int FindMax(int n1, int n2) if (n1 gt n2)
return n1 else
return n2
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 ltiostreamgt using namespace std /
Function Declarations / int FindMax(int n1, int
n2) int main() int i, j int k
cingtgtigtgtj k FindMax( i, j )
return 0 / Function Definitions / int
FindMax( int n1, int n2 ) if (n1 gt n2)
return n1 else
return n2
Actual parameter list
Call a Value-returning Function
Function Name
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 ltiostreamgt using namespace std /
Function Declarations / int FindMax(int n1, int
n2) int main() int i, j int k
cingtgtigtgtj k FindMax( i, j )
return 0 / Function Definitions / int
FindMax( int n1, int n2 ) if (n1 gt 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 ltiostreamgt using namespace std /
Function Declarations / int FindMax(int n1, int
n2) int main() int i, j int k
cingtgtigtgtj k FindMax( i, j )
return 0 / Function Definitions / int
FindMax( int n1, int n2 ) if (n1 gt 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 ltiostreamgt using namespace std /
Function Declarations / int FindMax(int n1, int
n2) int main() int i, j int k
cingtgtigtgtj k FindMax( i, j )
return 0 / Function Definitions / int
FindMax( int n1, int n2 ) if (n1 gt n2)
return n1 else
return n2
22
End of lecture 16
  • Thank you!
Write a Comment
User Comments (0)
About PowerShow.com