Chapter%208%20Functions%20in%20Depth - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter%208%20Functions%20in%20Depth

Description:

Chapter 8. Functions eliminate the need for duplicate statements within a program. ... Chapter 8. A function prototype is a model of the interface to the function. ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 28
Provided by: andrewst3
Category:

less

Transcript and Presenter's Notes

Title: Chapter%208%20Functions%20in%20Depth


1
Chapter 8Functions in Depth
2
Chapter 8
  • A programmer-defined function is a block of
    statements, or a subprogram, that is written to
    perform a specific task required by you, the
    programmer.

3
Chapter 8
  • Functions eliminate the need for duplicate
    statements within a program. Given a task to be
    performed more than once, the statements are
    written just once for the function. Then, the
    function is called each time the task must be
    performed.
  • Functions make the program easier to design,
    since the program behaviors (algorithms) are
    modularized as individual functions, whose
    combined execution solves the problem at hand.

4
Chapter 8
  • Functions make the program easier to code and
    test, since you can concentrate on a single
    function at a time, rather than one big main()
    function.
  • Functions allow for software reusability, where
    existing functions can be reused to create new
    programs.
  • Functions make the program more clear and
    readable, thus making the program easier to
    maintain.

5
Chapter 8
  • Functions provide the basis for structured as
    well as object-oriented programming

6
Chapter 8
  • A non-void function is a function that, when
    called, returns a single value to the calling
    program.

7
Chapter 8
  • Non-void Function Format
  • //FUNCTION HEADER
  • return data type function name (parameter list)
  • //BEGIN FUNCTION STATEMENT BLOCK
  • //LOCAL VARS
  • local variables should go here
  • //FUNCTION STATEMENTS or BODY
  • statement 1
  • statement 2
  • statement n
  • return value
  • //END FUNCTION STATEMENT BLOCK

8
Chapter 8
  • The function header forms the interface between
    the calling program and the function.

9
Chapter 8
  • The function header consists of a return type, if
    any, the function name, and a parameter listing.

10
Chapter 8
  • The value returned by a non-void function
    replaces the function name wherever the name is
    used in the calling program.Examples
  • result cube(a)
  • cout ltlt cube(a)
  • solution 2 ? cube(a) 5

11
Chapter 8
  • Arguments are values/variables used within the
    function call, and parameters are variables used
    within the function header that receive the
    argument values.

12
Chapter 8
  • A void function is a function that returns
    multiple values or performs some specific task,
    rather than returning a single value to the
    calling program.

13
Chapter 8
  • When calling a void function, simply list the
    function name and required arguments as a single
    statement within your program. Do not call a void
    function with an assignment operator or cout
    object as you do non-void functions.Example
    displayHeader()

14
Chapter 8
  • A value parameter provides for one-way
    communication of data from the calling program to
    the function.
  • A reference parameter provides two-way
    communication of data between the calling program
    and function.

15
Chapter 8
  • Locating Functionsin a Program

16
Chapter 8
  • A function prototype is a model of the interface
    to the function.
  • Simply copy/paste the function header and add a
    semicolon to code the prototype.
  • Locate your function prototypes after the
    preprocessor directives and before main().

17
Chapter 8
  • A default parameter is a function parameter that
    is assigned a default value in either the
    function prototype or the function header, but
    not both. Exampleint volume(int length, int
    width 5, int height 2)

18
Chapter 8
  • An overloaded function exhibits different
    behavior with a different number of arguments or
    argument data types. Example
  • int calculateArea(int)
  • int calculateArea(int, int)
  • double calculateArea(double)

19
Chapter 8
  • A local variable is a variable that is defined
    within a specific block of code, such as a
    function.
  • The scope of a variable refers to the largest
    block in which a given variable is accessible.
  • Always define your variables as locally as
    possible within a given function block. If
    variables need to be shared among functions, pass
    them.

20
Chapter 8
  • Recursion is a process whereby an operation calls
    and clones itself until a terminating condition
    is reached.
  • Examples of recursive operations include compound
    interest calculation, summation, and factorial,
    just to mention a few.

21
Chapter 8
  • Calculating a Monthly Compounded Bank Balance
    using Recursion
  • If n is 0 balance(n) deposit
  • Else
  • balance(n) (1rate/12/100)?
    balance(n1)(Where n is the month for which the
    balance must be determined.)

22
Chapter 8
  • A recursive function must always reach a
    terminating condition. If it does not, the
    function will keep calling itself forever,
    resulting in a memory overflow run-time error.

23
Chapter 8
  • double balance(double deposit, int month, double
    rate)
  • if (month 0)
  • return deposit
  • else
  • return (1 rate / 12 / 100)
    balance(deposit,month 1,rate)
  • //END balance()

24
Chapter 8
25
Chapter 8
  • If n is 1
  • sum(n) 1
  • Else
  • sum(n) n sum (n 1)

26
Chapter 8
  • int sum(int n)
  • if (n 1)
  • return 1
  • else
  • return n sum(n ? 1)
  • //END sum()

27
Chapter 8
  • int sum(int n)
  • int subTotal 0 //SUM SUBTOTAL
  • for (int count 1 count lt n count)
  • subTotal subTotal count
  • return subTotal
  • //END sum()
Write a Comment
User Comments (0)
About PowerShow.com