Top Banner

of 27

ism 3230-ch7

Apr 14, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/27/2019 ism 3230-ch7

    1/27

    User-Defined Functions II

    ISM 3230 Intro to Comp Systems and Software Development

    Dr. Caryn Conley

  • 7/27/2019 ism 3230-ch7

    2/27

    Chapter Goals

    Chapter 7 Objectives

    Learn how to construct and use void functions in a program

    Discover the difference between value and reference parameters

    Explore reference parameters and value-returning functions

    Learn about the scope of an identifier

    Examine the difference between local and global identifiers

  • 7/27/2019 ism 3230-ch7

    3/27

    Void Functions

    What are void fun ct ions?

    Void functions and value-returning functions have similarstructures

    Both have a heading and a body

    In our class, we will place ALL user-defined functions (void and

    value-returning) after the function main When we do this, we must place the function prototype

    before the function main

    Remember, the function prototype is like a declaration of thefunction so that we can compile our program

    A void function does not have a return type - DIFFERENT Therefore, our return statement will not have a value:

    return;

    Formal parameters are optional

    A call to a void function is a stand-alone statement - DIFFERENT

  • 7/27/2019 ism 3230-ch7

    4/27

    Void Functions (cont)

    Void fun ct ion syntax No parameters

    Function definition syntax:

    void is a reserved word

    Function call syntax:

  • 7/27/2019 ism 3230-ch7

    5/27

    Void Functions (cont)

    Void fun ct ion syntax With parameters

    Function definition syntax:

    Formal parameter list syntax:

    Function call syntax:

    Actual parameter list syntax:

  • 7/27/2019 ism 3230-ch7

    6/27

    Void Functions (cont)

    Types of parameters

    We use parameters to allow our functions to communicate witheach other

    There are two types of parameters to enable different types ofcommunication: value (what we learned last week) and

    reference (new concept for today) Value parameter: a formal parameter that receives a copy

    of the content of corresponding actual parameter

    Reference parameter: a formal parameter that receivesthe location (memory address) of the corresponding actualparameter

  • 7/27/2019 ism 3230-ch7

    7/27

    Value Parameters

    What are value parameters?

    If a formal parameter is a value parameter

    The value of the corresponding actual parameter is copiedinto it

    The value parameter has its own copy of the data

    During program execution The value parameter manipulates the data stored in its own

    memory space

    This means the value stored in the actual parameter CANNOTbe changed by the function

    This is how we talked about parameters last week

  • 7/27/2019 ism 3230-ch7

    8/27

    Reference Variables as Parameters

    What are reference variables?

    If a formal parameter is a reference parameter

    It receives the memory address of the corresponding actualparameter

    A reference parameter stores the address of the corresponding

    actual parameter During program execution to manipulate data

    The address stored in the reference parameter directs it tothe memory space of the corresponding actual parameter

    This means the value stored in the actual parameter CAN be

    changed by the function!!

  • 7/27/2019 ism 3230-ch7

    9/27

    Reference Variables as Parameters (cont)

    What are reference variables? (con t)

    Reference parameters can:

    Pass one or more values from a function

    Change the value of the actual parameter

    Reference parameters are useful in three situations:

    Returning more than one value

    Changing the actual parameter

    When passing the address would save memory space andtime

  • 7/27/2019 ism 3230-ch7

    10/27

    Reference vs Value Parameters

    Syntax

    How do we know if it is a value or a reference parameter?

    Look at the function heading!

    Value parameters:

    In the list of formal parameters, list the data type and

    the parameter name Example: int absoluteValue(int nNumber)

    Reference parameters :

    In the list of formal parameters, list the data type

    immediately followed by & and the parameter name

    Example: void absoluteValue(int&nNumber)

    Tiny symbol. HUGE

    implications.

  • 7/27/2019 ism 3230-ch7

    11/27

    Parameter Types and Memory Allocation

    Memo ry impl icat ions

    When a function is called

    Memory for its formal parameters and variables declared inthe body of the function (called local variables) is allocated inthe function data area

    In the case of a value parameter The value of the actual parameter is copied into the memory

    cell of its corresponding formal parameter

    In the case of a reference parameter

    The address of the actual parameter passes to the formal

    parameter Content of formal parameter is an address

    During execution, changes made by the formal parameterpermanently change the value of the actual parameter

    NOTE: Stream variables (e.g., ifstream) should be passed by

    reference to a function

  • 7/27/2019 ism 3230-ch7

    12/27

    #include

    using namespace std;

    int absoluteValue (int nNumber); //Function prototype

    int main()

    {

    int nAbs, nAbsolute;

    cout > nAbs;nAbsolute = absoluteValue(nAbs); //Call absoluteValue

    cout

  • 7/27/2019 ism 3230-ch7

    13/27

    User-Defined Functions (cont)

    Pseudocode for main funct ion

    Declare constants (if necessary)

    Declare variables

    Number

    Absolute

    Prompt user Enter an integer:

    Get Number from user

    Set Absolute = Call absoluteValue passing Number

    Print The absolute value of the number entered: Absolute

    Call to a value-returning function

  • 7/27/2019 ism 3230-ch7

    14/27

    User-Defined Functions (cont)

    Pseudocode for absoluteValue fun ct ion

    Value parameters:

    Number

    Reference parameters:

    None

    Return:

    Number

    Declare constants (if necessary)

    Declare variables (if necessary)

    If Number < 0

    Calculate Number = -Number

    Return Number

    NEW!

  • 7/27/2019 ism 3230-ch7

    15/27

    #include

    using namespace std;

    void absoluteValue (int&nNumber); //Function prototype

    int main()

    {

    int nAbs = 0;

    cout > nAbs;absoluteValue(nAbs); //Call absoluteValue

    cout

  • 7/27/2019 ism 3230-ch7

    16/27

    User-Defined Functions (cont)

    A fun ct ion w ith reference parameters in a f low c hart

    Number

    Start

    Prompt

    user forumber

    Getnumber

    from user

    Callabsolute

    Value

    EndPrint

    number

    main

    NEW!

  • 7/27/2019 ism 3230-ch7

    17/27

    User-Defined Functions (cont)

    A fun ct ion w ith reference parameters in a f low c hart (cont)

    number

    Start

    End

    absoluteValue

    ifnumber

    < 0

    Calculatenumber

    yes

    no

  • 7/27/2019 ism 3230-ch7

    18/27

    User-Defined Functions (cont)

    Pseudocode for main funct ion

    Declare constants (if necessary)

    Declare variables

    Number

    Prompt user Enter an integer:

    Get Number from user

    Call absoluteValue passing Number

    Print The absolute value of the number entered: Number

    Call to a void function

  • 7/27/2019 ism 3230-ch7

    19/27

    User-Defined Functions (cont)

    Pseudocode for absoluteValue fun ct ion

    Value parameters:

    None

    Reference parameters:

    Number

    Return:

    None

    Declare constants (if necessary)

    Declare variables (if necessary)

    If Number < 0

    Calculate Number = -Number

    Return

    NEW!

  • 7/27/2019 ism 3230-ch7

    20/27

    Scope of an Identifier

    What is scope?

    The scope of an identifier refers to where in the program anidentifier is accessible

    Local identifier: identifiers declared within a function (or block)

    Global identifier: identifiers declared outside of every function

    definition C++ does not allow nested functions

    The definition of one function cannot be included in the bodyof another function

    This will cause a compiler error!

  • 7/27/2019 ism 3230-ch7

    21/27

    Scope of an Identifier (cont)

    #include

    using namespace std;

    const double RATE = 10.50;

    int nGuests = 0;

    void calculateCharge(int nNites, double& dCharge);

    int main()

    {

    int nNights = 0;

    double dAmountDue = 0.0;

    return 0;

    } //End main method

    void calculateCharge(int nNites, double& dCharge)

    {

    double dTemp = 0.0;

    return;

    } //End main method

    Global identifiers

    Local identifiers Canonly see in main

    Local identifiers Can only see incalculateCharge

  • 7/27/2019 ism 3230-ch7

    22/27

    Scope of an Identifier (cont)

    Imp l icat ions of p revious sl ide

    Global identifiers:

    Can use these identifiers in either main or calculateCharge:

    RATE

    nGuests

    Local identifiers:

    Can use these identifiers ONLY in main:

    nNights

    dAmount

    Can use these identifiers ONLY in calculateCharge:

    dTemp

    dCharge

    nNites

  • 7/27/2019 ism 3230-ch7

    23/27

    Scope of an Identifier (cont)

    Some C++ rules

    Global identifiers (such as variables) are accessible by a functionif:

    The function name is different from the identifier

    All parameters of the function have names different than the

    name of the identifier, and All local identifiers (such as local variables) have names

    different than the name of the identifier

    The scope of a function name is the same as the scope of aglobal variable

  • 7/27/2019 ism 3230-ch7

    24/27

    Scope of an Identifier (cont)

    Som e ISM 3230 rules

    The only global identifiers you may use in your program include

    Constants (always declared before any function)

    User-defined functions (function prototype always declaredbefore any function definition)

    Declaring local variables All local variables declared at the beginning of the function

    body (i.e. at the top of the function)

    All identifiers in your program, including functionparameters, should have unique names (i.e. do not usenNum1

    inmain

    and anothernNum1

    inabsoluteValue

    ) Using the return statement

    All functions must include one and only one return

    statement

    Value returning: return;

    Void functions: return;

  • 7/27/2019 ism 3230-ch7

    25/27

    Class Exercise

    No coding tod ay

    Refer to the handout, and identify the following information ineach of the examples

    Function prototype, function heading, function body andfunction definitions

    Function call statements, formal parameters, and actualparameters

    Value parameters and reference parameters

    Local variables and global variables

    Mark the order in which the statements will execute

    Identify the final values of each variable at the end ofprogram execution

    Identify the output of each program

    For the last example only, create a flow chart and

    pseudocode for each function

  • 7/27/2019 ism 3230-ch7

    26/27

    Summary

    Summary o f Chapter 7

    Void function: does not have a data type

    A return statement without any value can be used in a voidfunction to exit it early

    The heading starts with the word void

    To call the function, you use the function name together withthe actual parameters in a stand-alone statement

    Two types of formal parameters:

    Value parameters

    Reference parameters

  • 7/27/2019 ism 3230-ch7

    27/27

    Summary (cont)

    Summary of Chapter 7 (con t)

    A value parameter receives a copy of its corresponding actualparameter

    A reference parameter receives the memory address of itscorresponding actual parameter

    If a formal parameter needs to change the value of an actualparameter, you must declare this formal parameter as areference parameter in the function heading

    Variables declared within a function are called local variables

    Variables declared outside of every function definition are globalvariables