You are on page 1of 27

ISM 3230

Intro to Comp Systems and Software Development

User-Defined Functions II

Dr. Caryn Conley

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

Void Functions (cont)


Void function syntax No parameters
Function definition syntax:

void is a reserved word


Function call syntax:

Void Functions (cont)


Void function syntax With parameters
Function definition syntax:

Formal parameter list syntax:

Function call syntax:

Actual parameter list syntax:

Void Functions (cont)


Types of parameters
We use parameters to allow our functions to communicate with each other There are two types of parameters to enable different types of communication: 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 receives the location (memory address) of the corresponding actual parameter

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

This is how we talked about parameters last week

Reference Variables as Parameters


What are reference variables?
If a formal parameter is a reference parameter

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!!

Reference Variables as Parameters (cont)


What are reference variables? (cont)
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 and time

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.

Parameter Types and Memory Allocation


Memory implications
When a function is called

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

Functions with Value Parameters


#include <iostream> using namespace std; int absoluteValue (int nNumber); //Function prototype int main() { int nAbs, nAbsolute; cout << Enter an integer: ; cin >> nAbs; nAbsolute = absoluteValue(nAbs); //Call absoluteValue cout << The absolute value of the number entered: << nAbsolute; return 0; } //end main function int absoluteValue (int nNumber) { if (nNumber < 0) { nNumber = -nNumber; } //end if number is negative return nNumber; } //end absoluteValue function //Function defn value parameter

User-Defined Functions (cont)


Pseudocode for main function
Declare constants (if necessary)

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

User-Defined Functions (cont)


Pseudocode for absoluteValue function
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!

Functions with Reference Parameters


#include <iostream> using namespace std; void absoluteValue (int& nNumber); //Function prototype int main() { int nAbs = 0; cout << Enter an integer: ; cin >> nAbs; absoluteValue(nAbs); //Call absoluteValue cout << The absolute value of the number entered: << nAbs; return 0; } //end main function void absoluteValue (int& nNumber) { if (nNumber < 0) { nNumber = -nNumber; } //end if number is negative return; } //end absoluteValue function //Function defn reference param

User-Defined Functions (cont)


A function with reference parameters in a flow chart
main

Start
Prompt user for umber Get number from user Number Call absolute Value Print number

NEW!

End

User-Defined Functions (cont)


A function with reference parameters in a flow chart (cont)
absoluteValue Start if number <0 yes Calculate number no number

End

User-Defined Functions (cont)


Pseudocode for main function
Declare constants (if necessary)

Declare variables Number


Prompt user Enter an integer:

Get Number from user Call absoluteValue passing Number Call to a void function Print The absolute value of the number entered: Number

User-Defined Functions (cont)


Pseudocode for absoluteValue function
Value parameters:

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!

Scope of an Identifier (cont)


#include <iostream> using namespace std; const double RATE = 10.50;

int nGuests = 0;
int main() { int nNights = 0;

Global identifiers

void calculateCharge(int nNites, double& dCharge);

double dAmountDue = 0.0;


return 0; } //End main method

Local identifiers Can only see in main

void calculateCharge(int nNites, double& dCharge) {

double dTemp = 0.0;


return; } //End main method

Local identifiers Can only see in calculateCharge

Scope of an Identifier (cont)


Implications of previous slide
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

Scope of an Identifier (cont)


Some C++ rules
Global identifiers (such as variables) are accessible by a function if: 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 a global variable

Scope of an Identifier (cont)


Some 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 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

You might also like