Chapter 4 Subprograms: Functions for Problem Solving - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Chapter 4 Subprograms: Functions for Problem Solving

Description:

... letters, numbers, and underscores but start with a letter ... Example: int cube(int x) Reference Parameters. A method to return values to the calling function. ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 29
Provided by: jimvand
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4 Subprograms: Functions for Problem Solving


1
Chapter 4 Subprograms Functions for Problem
Solving
2
Modular Programming
  • Each section of the code has a specific purpose
  • Each specific purpose may be reusable by other
    programs
  • To do so, we create functions to be shared
  • Similarly, within a program, we would like to
    employ the function concepts

3
Declarations of Variables and Constants
  • Local variables (constants) are variables used
    within only one function.
  • Local variables should be declared in the
    function used
  • Global variables (constants) are variables that
    can be used by all functions in a program
  • Global variables should be declared after the
    prepecessor statements (include)

4
Functions
  • All programs have a main function
  • Functions should be grouped together and placed
    either before or after the main function
  • My preference is after the main function
  • However, since the main function will refer to
    other functions, the other function must be
    declared as a global variable

5
Declaration of Functions
  • ltreturn typegt ltfunction namegt(ltlist of
    parametersgt)
  • Return type is the data type that will be
    returned to the calling function
  • Valid entries are int, float, char, bool, or void
    (no return value)
  • Function name is any valid identifier
  • combination of letters, numbers, and underscores
    but start with a letter
  • list of parameters are values to be passed into
    the function
  • Consist of data type and identifier
  • If more than one, separate by comma
  • Example int cube(int x)

6
Example
  • float example_function(int x)
  • BODY
  • Return some_value

7
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • //Main Function
  • void main()
  • //Declare Variables
  • float length,width,area
  • //Input Section
  • coutltlt"Enter The Length and Width "
  • cingtgtlengthgtgtwidth
  • //Calculation Section
  • arealengthwidth
  • //Print Headings

8
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • float length, width, area
  • float get_record()
  • //Main Function
  • void main()
  • get_record()
  • //Calculation Section
  • arealengthwidth
  • //Print Headings
  • coutltltsetw(12)ltlt"Length"ltltsetw(12)ltlt"Width
    "ltltsetw(12)ltlt"Area"ltltendl
  • //Print Detail Lines
  • coutltltsetprecision(2)ltltsetiosflags(iosfi
    xediosshowpoint)

9
  • //A Program to Cube an Integer
  • includeltiostreamgt
  • int cube(int)
  • void main()
  • int in_integer
  • int cubed_integer
  • coutltlt"Enter in Integer and hit Enter"ltltendl
  • cingtgtin_integer
  • cubed_integercube(in_integer)
  • coutltltendlltlt"The cube of "ltltin_integerltlt" is
    "ltltcubed_integer
  • int cube(int x)

10
  • //A Program to Cube an Integer
  • includeltiostreamgt
  • int cube(int)
  • void main()
  • int in_integer
  • coutltlt"Enter in Integer and hit Enter"ltltendl
  • cingtgtin_integer
  • coutltltendlltlt"The cube of "ltltin_integerltlt" is
    "ltltcube(in_integer)
  • int cube(int x)
  • return xxx

11
  • include ltiostream.hgt
  • include ltiomanip.hgt
  • float length, width, area
  • float get_record()
  • float calculate_area(float x,float y)
  • void print_heading()
  • void print_detail()
  • void main()
  • get_record()
  • calculate_area(length,width)
  • print_heading()
  • print_detail()
  • float get_record()

12
  • float calculate_area(float x, float y)
  • arealengthwidth
  • return area
  • void print_heading()
  • coutltltsetw(12)ltlt"Length"ltltsetw(12)ltlt"Width"ltlts
    etw(12)ltlt"Area"ltltendl
  • void print_detail()
  • coutltltsetprecision(2)ltltsetiosflags(iosfixed
    iosshowpoint)
  • coutltltsetw(12)ltltlengthltltsetw(12)ltltwidthltltsetw
    (12)ltltarealtltendl

13
Declaration of Functions
  • ltreturn typegt ltfunction namegt(ltlist of
    parametersgt)
  • Return type is the data type that will be
    returned to the calling function
  • Valid entries are int, float, char, bool, or void
    (no return value)
  • Function name is any valid identifier
  • combination of letters, numbers, and underscores
    but start with a letter
  • list of parameters are values to be passed into
    the function
  • Consist of data type and identifier
  • If more than one, separate by comma
  • Example int cube(int x)

14
Reference Parameters
  • A method to return values to the calling
    function.
  • Lets examine the program for calculating the
    area of a rectangle.

15
Area of a Rectangle
  • includeltiostream.hgtincludeltiomanih.hgt
  • void main()float length, width, area
  • coutltltEnter the length\ncingtgtlengthcoutltlt\n
    Enter the width\n
  • arealengthwidth
  • coutltltsetiosflags(iosshowpointiosfixed)ltltsetp
    recision(2)coutltltlength ltltlengthltltendlcoutltlt
    width ltltwidthltltendlcoutltltarea ltltarealtltendl

16
Area of a Rectangle
  • get_data
  • calc_area
  • out_results

17
Reference Parameters
  • The syntax is
  • data_type function_name (data_type
    parameter_name1,...)
  • Examplevoid get_data(float length, float
    width)

18
Area of a Rectangle
  • includeltiostream.hgtincludeltiomanih.hgt
  • void main()
  • float length, width, area void
    get_data(float length, float width) float
    calc_area(float length, float width) void
    out_results(float length, float width,
    float area)
  • get_data(length, width)
    calc_area(length, width)
    out_results(length, width, area)

19
get_data
  • void get_data(float length, float width)
    coutltltEnter the length\n cingtgtlength
    coutltlt\nEnter the width\n cingtgtwidth

20
calc_area
  • float calc_area(float length, float width)
    arealengthwidth return area

21
out_results
  • void out_results(float length, float width,
    float area)coutltltsetios
    flags(iosshowpointiosfixed)
    ltltsetprecision(2)coutltltlength
    ltltlengthltltendlcoutltltwidth ltltwidthltltendlcout
    ltltarea ltltarealtltendl

22
4.6 Programmer-Defined Libraries
23
Library Header File
  • Need
  • library header file
  • library implementation file

24
Header file
  • extension is .h
  • ifndef file_identifier
  • define file_identifier
  • function declaration(s)
  • endif
  • The ifndef...endifasks if the file identifier
    has been defined. If it has, the library file has
    already preprocessed the current inclusion and
    jumps to endif.
  • Header file contains the name of the functions.

25
Library Implementation File
  • extension is .cxx
  • include file_identifier
  • file_identifier is the header file and must be in
    quotations.
  • This file actually defines the function.

26
Example - a switch program
  • The header file
  • my_input.h
  • ifndef my_inputdefine my_inputvoid
    do_switch(int x)endif

27
Example - a switch program
  • The function file
  • my_input.cxx
  • include my_input.h void do_switch(int
    x)switch(x)case 1 coutltltThis is the first
    number\n breakcase 2 coutltltThis
    is the second number\n
    breakdefault coutltltThis number is out of
    range\n

28
Example - a switch program
  • include my_input.hvoid main ()int
    in_number0while(in_number!-999)coutltltInput
    an integer or -999 to quit\ncingtgtin_numberdo
    _switch(in_number)
Write a Comment
User Comments (0)
About PowerShow.com