Professional Documents
Culture Documents
User-Defined Functions II
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
Void Functions
What are void functions?
Void functions and value-returning functions have similar structures 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 the function 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
Value Parameters
What are value parameters?
If a formal parameter is a value parameter
The value of the corresponding actual parameter is copied into 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 CANNOT be changed by the function
It receives the memory address of the corresponding actual parameter 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 to the memory space of the corresponding actual parameter This means the value stored in the actual parameter CAN be changed by the function!!
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 and time
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)
Memory for its formal parameters and variables declared in the body of the function (called local variables) is allocated in the 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 parameter permanently change the value of the actual parameter NOTE: Stream variables (e.g., ifstream) should be passed by reference to a function
Declare variables Number Absolute Prompt user Enter an integer: Call to a value-returning function Get Number from user Set Absolute = Call absoluteValue passing Number Print The absolute value of the number entered: Absolute
Number Reference parameters: None Return: Number Declare constants (if necessary) Declare variables (if necessary)
If Number < 0 Calculate Number = -Number Return Number
NEW!
Start
Prompt user for umber Get number from user Number Call absolute Value Print number
NEW!
End
End
Get Number from user Call absoluteValue passing Number Call to a void function Print The absolute value of the number entered: Number
None Reference parameters: Number Return: None Declare constants (if necessary) Declare variables (if necessary)
If Number < 0 Calculate Number = -Number Return
NEW!
Scope of an Identifier
What is scope?
The scope of an identifier refers to where in the program an identifier 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 body of another function This will cause a compiler error!
int nGuests = 0;
int main() { int nNights = 0;
Global identifiers
Constants (always declared before any function) User-defined functions (function prototype always declared before 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 function parameters, should have unique names (i.e. do not use nNum1 in main and another nNum1 in absoluteValue) Using the return statement All functions must include one and only one return statement Value returning: return <value>; Void functions: return;
Class Exercise
No coding today
Refer to the handout, and identify the following information in each of the examples Function prototype, function heading, function body and function definitions Function call statements, formal parameters, and actual parameters
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 of program execution Identify the output of each program
For the last example only, create a flow chart and pseudocode for each function
Summary
Summary of Chapter 7
Void function: does not have a data type
A return statement without any value can be used in a void function to exit it early The heading starts with the word void To call the function, you use the function name together with the actual parameters in a stand-alone statement Two types of formal parameters: Value parameters Reference parameters
Summary (cont)
Summary of Chapter 7 (cont)
A value parameter receives a copy of its corresponding actual parameter A reference parameter receives the memory address of its corresponding actual parameter If a formal parameter needs to change the value of an actual parameter, you must declare this formal parameter as a reference parameter in the function heading Variables declared within a function are called local variables Variables declared outside of every function definition are global variables