Lecture 7 User Defined Functions - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Lecture 7 User Defined Functions

Description:

Formal parameter. Each item of information received by a function. 9/24/09 ... Data type of formal parameters must match data type of actual arguments ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 42
Provided by: jimand
Category:

less

Transcript and Presenter's Notes

Title: Lecture 7 User Defined Functions


1
Lecture 7User Defined Functions
  • ENGR17 Engineering Programming
  • Section 1
  • Fall 2001

9/28/01
2
Outline
  • Creating and invoking void functions
  • Passing information by value
  • Passing information by reference
  • Creating a function that returns a value
  • Passing a string to a function

3
Programmer-Defined Function
  • Can be either
  • Value-returning function
  • Void function
  • Called in exactly the same way as a built-in
    function
  • Void Function
  • A function that does not return a value
  • Could be used to display information
  • Could be used to output to a file
  • And other uses

4
Example Program
  • Sample OutputEnter a number 4-----------------
    ------------------------------------------------S
    quare and Square Root CalculationsSquare
    16Square Root 2Press any key to continue_

5
(No Transcript)
6
Void Function Definition
  • Defines the functions task
  • Is entered in programmer-defined section of
    program
  • Uses same naming rules as naming variables
  • Function header begins with keyword void
  • Enclose statements that comprise the function in
    a set of braces
  • Formal parameter
  • Each item of information received by a function

7
Function Definition
8
Calling a Void Function
  • Include a function definition and function
    call(s) in a program
  • Enter function prototype above the main function
  • Function prototype and function definitions
    header must begin with keyword void
  • Use the functions name and actual arguments, if
    any, as a separate statement in the program

9
Calling a Void Function
10
Calling a Void Function
11
Using Function Prototypes
  • Statements that tell C the following
  • The functions name
  • The data type of its return value
  • The data type of each of its formal parameters

12
3 Parts of a Function
  • include ltiostream.hgt
  • include ltmath.hgt
  • // function prototypes
  • void displayLine()
  • void main ()
  • //declare and initialize variables
  • float numberInput (float) 0.0
  • displayLine()
  • void displayLine()

1. Prototype
2. Call
3. Definition
13
Passing Information to a Function
  • Include one or more actual arguments in the
    function calls argumentlist
  • Typically, information passed to a function is
    one of the following
  • A literal constant
  • The contents of a variable
  • The address of a variable in memory
  • Used when the function needs to process
    information

14
Passing Information to a Function
  • Allow a function to receive information passed to
    it by including formal parameter(s) in function
    headers parameterlist
  • Data type and name of formal parameters must be
    explicitly stated in function header
  • Data type of formal parameters must match data
    type of actual arguments
  • Order in which formal parameters are listed in
    function header must match order of actual
    arguments listed in the function

15
Passing Information to a Function
Argument List
Parameter List
16
(No Transcript)
17
Passing Information to a Function
18
The Scope of a Variable
  • Indicates which portions of the program can use
    the variable
  • Is determined by where the variable is declared
    in the program
  • Local
  • Declared in a statement block known only to
    function or statement block in which they are
    declared
  • Global (avoid using)
  • Declared outside of any function in the program
  • Allow unintentional errors
  • Lifetime of a Variable
  • Indicates how long the variable remains in the
    computers memory

19
Passing Variables by Value
  • C passes only the contents of the variable to
    the receiving function
  • Receiving function does not have access to the
    variable in memory, so it cannot change the
    variables contents
  • Used when the receiving function needs to know,
    but not change, the value stored inside the
    variable
  • Unless specified otherwise, all variables in C
    are passed by value

20
Passing Variables by Reference
  • C passes the variables address in memory
  • Contents of the variable can be changed by the
    receiving function
  • Used when the receiving function needs to change
    the contents of the variable
  • Must include an ampersand, called the address-of
    operator, before the corresponding formal
    parameters name in the receiving functions
    parameterlist

21
Pass Variables by Value and Reference
  • To pass the three variables, modify the
    following
  • Function prototype
  • Function call
  • Function definition

22
(No Transcript)
23
Pass Variables by Value and Reference
24
Pass Variables by Value and Reference
25
(No Transcript)
26
Pass Variables by Value and Reference
27
Pass Variables by Value and Reference
28
Summary of Void Functions
  • Creating and invoking void functions
  • Passing information by value
  • Passing information by reference
  • Variable Scope

29
Value-returning Functions
  • Similar to creating and invoking a void function
  • Enter same three items of information
  • A function prototype
  • One or more function calls
  • A function definition

30
Value-returning Functions
  • Function prototype and function definitions
    header must begin with a data type
  • Function definition must end with a return
    statement that returns a value to the caller
  • A call to a value-returning function does not
    typically appear as a statement by itself, but as
    a part of another statement
  • Can return only one value

31
Value-returning Functions
32
Value-returning vs. Void Functions
33
continued
34
Value-returning Functions
35
Passing a String to a Function
  • Strings are always passed by reference
  • Thus, strings can be modified in the function
  • Examplevoid main () char first50
    Jon char last50 Smith char name50
    make_string(first, last, name)void
    make_string(char first , char last , char
    name ) strcpy(name, last) strcat(name, ,
    ) strcat(name, first)

Dont need string lengths in header
36
Summary of Functions
  • Creating and invoking void functions
  • Passing information by value
  • Passing information by reference
  • Variable Scope
  • Creating a function that returns a value
  • Passing a string to a function

37
Questions
  • What comes before main() when you use functions?
  • Function prototype
  • What is within main() when you use a function?
  • Function call
  • What is after main() when you use a function?
  • Function definition
  • What is the name for the information within the
    () of a function call?
  • Argument list
  • What is the name for the information within the
    () of a function header in function definition?
  • Parameter list

38
Questions
  • Give the function header for a void function
    foo with no arguments
  • void foo(void)
  • Give the function header for a function foo
    that returns and integer but has no arguments
  • int foo(void)
  • Give the function header for a function foo
    that returns and integer and is passed the
    integer a by value
  • int foo(int a)
  • Give the function header for a void function
    foo that is passed the float a by reference
    and the char b by value
  • void foo(float a, char b)

39
Example
  • Rewrite Lab01 using functions
  • Design a program to compute the area and
    perimeter of a rectangle. The user will input the
    height and width (in meters) of the rectangle and
    the program will print the area and perimeter to
    the screen.
  • Use a value returning function with pass by value
    to calculate the area
  • Use a void function with pass by reference and
    pass by value to calculate the perimeter

40
Code (1)
  • include ltiostream.hgt
  • float calc_area(float height, float width)
  • void calc_perimeter(float perimeter, float
    height, float width)
  • int main ()
  • float height
  • float width
  • float area
  • float perimeter
  • // get height and width from user
  • cout ltlt "Enter height "
  • cin gtgt height
  • cout ltlt "Enter width "
  • cin gtgt width
  • // calculate area
  • area calc_area(height,width)

41
Code (2)
  • float calc_area(float height, float width)
  • return height width
  • void calc_perimeter(float perimeter, float
    height, float width)
  • perimeter 2height 2width
Write a Comment
User Comments (0)
About PowerShow.com