Functions 2: Void (NonValue-Returning) Functions

Void (NonValue-Returning) functions:

Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value. You may or may not use the return statement, as there is no return value. Even without the return statement, control will return to the caller automatically at the end of the function. A good utilization of a void function would be to print a header/footer to a screen or file.

Remember: there are two kinds of subprograms that the C++ language utilizes: value-returning functions and void functions. Both value-returning functions and void functions receive values through their parameter lists. A value-returning function can only return one value to the calling environment. The caller invokes (calls) a value-returning function by using its name and argument list in an expression (i.e., 1. assignment, 2. output, or as an 3. argument in another function call):

//value-returning function call (assignment):
y = 2.0 * sqrt(x);
In contrast, a void function (method or procedure, in other languages) does not return a function value. Nor is it called from within an expression. Instead, the function call appears as a complete, stand-alone statement. One example is the get function associated with the istream and ifstream classes:
//void function call:
cin.get();
Another example:
//void function call:
printName(name);
Bjarne Stroustrup's C++ Glossary
Similarities Between Value-Returning and Void (NonValue-Returning) functions: Differences Between Value-Returning and Void (NonValue-Returning) functions: ***Void function calls can ONLY be used w/in stand-alone statements.

***Conversely, value-returning function calls can be used in
  1. output: e.g., cout statements
  2. assignment
  3. arguments in other function calls

Function Parameters

Function Parameter Types:
Two Types of Function Parameters:

  1. Value Parameter: formal parameter receives copy of content of corresponding actual parameter
  2. Reference Parameter: formal parameter receives location (memory address) of corresponding actual parameter
Using Value and Reference Parameters:
Value Parameters: Void Function Definition Using Value Parameters Example: Reference Parameters: Void Function Definition Using Reference Parameters Example: Function Calls Using Value and Reference Parameter Examples:
Void function call using value parameters (can use expression, constant, or variable): Void function call using reference parameters (can NOT use expression or constant, ONLY variables):

Function Parameters And Memory Allocation

When a function is called:

Summary of pass by value vs. pass by reference:
Pass by value:
  1. Makes copy of actual parameter
  2. Passes copy of contents
  3. Original variable's contents DO NOT change
  4. How? Do nothing. Default.
  5. Safer
Pass by reference:
  1. Passes address of actual parameter
  2. Accesses original variable's contents (via address)
  3. Original variable's contents DO change
  4. How? Add ampersand (&) before parameter name in header and prototype
  5. More efficient

Note: Although the C++ language allows value-returning functions to have both value parameters and reference parameters, it is not recommended. By definition, a value-returning function returns a single value; this value is returned via the return statement. Use void functions with reference parameters to return (modify) more than one value.

Program demos void functions using value and reference parameters:

Void or Value-Returning Functions?

When to use Void or Value-Returning Functions:

When/How to Pass/Return Values Recommendations:
  1. Use pass-by-value to pass very small objects (e.g., 3 ints, floats, doubles, etc.)
  2. Use pass-by-const-reference to pass large objects (that do NOT need to be modified)
  3. Return result rather than modify object through reference argument (value-returning function)
  4. Use pass-by-reference ONLY when necessary

Scope

What is scope?

Scope (accessibility) rules: Namespace Scope: Global variable problems: Scope Summary Rules:
  1. Function names have global scope
  2. Function parameter's scope is identical to scope of local variable declared in outermost block of function body
  3. Global variable's (or constant's) scope extends from its declaration to end of program file, except as noted in rule 5
  4. Local variable's (or constant's) scope extends from its declaration to end of block where declared, including any nested blocks, except as noted in rule 5
  5. Identifier's scope does not include any nested block that contains a locally declared identifier with same name (local identifiers have name precedence)

Lifetime

What is lifetime?


Stub Functions

Using Stub Functions:

Stub functions may be used when testing programs. A stub function is a stripped-down, skeletal structure of the actual function. It does not implement the full details of the algorithm or function requirements. It does contain the parameter lists. Essentially, a stub is a dummy function with a limited body, usually just output statements that acknowledge the function was reached, and a return value (if any) of the correct type. Usually, the stub function's name and parameter list is the same as the function that will actually be called by the program being tested.


Function Overloading

What is Function Overloading:

Several functions with the same name is called function overloading. Generally, function overloading is used when different data types will be used with the same function. As an illustration, one function may accept integer values, while another can receive char or float data. Of course, you could implement the same functionality using a different function name whose parameters would receive the various data types--or, you can employ function overloading.

When overloading a function, dissimilar signatures (i.e., different "TON": type, order, or number) must be used for each overloaded version. A function signature consists of a list of data types of its parameters, as well as their order, and number (i.e., the number of parameters).

Lastly, a function differing only by return type, OR different parameter names is illegal (though, it is legal to use a different return type with a different parameter list--that is, different "TON." See below):

Function Overloading Example: Function Overloading Summary:

Functions with Default Parameters

Using Functions with Default Parameters:

When a function is called, the number of actual and formal parameters must be the same except in the case of default parameters. The value of a default parameter is specified when the function name appears for the first time (as in the prototype).

The following rules apply to default parameters:

Functions with Default Parameters Example:

Recursion

A Recursive Function:

Similarities between Recursion & Iteration: Drawbacks for Recursion: