Chapter Six

Ace your homework & exams now with Quizwiz!

When a function uses a mixture of parameters with and without default arguments, the parameters with default arguments must be defined last. • When an argument is left out of a function call (because it has a default value), all the arguments that come after it must be left out too.

// Function prototype void calcPay(int empNum, double payRate, double hours = 40.0); // The double hours must be defined after ALL parameters without default arguments. • The value of a default argument must be a literal value or a named constant.

The exit() function causes a program to terminate, regardless of which function or control mechanism is executing. When the exit function is called, it causes the program to stop, regardless of which function contains the call. When the exit function is encountered the program is ended immediately. #include <cstdlib> // Needed for the exit function

A C++ program stops executing when the return statement in function main is encountered. When other functions end, however, the program does not stop. Control of the program goes back to the place immediately following the function call. Sometimes, rare circumstances make it necessary to terminate a program in a function other than main . To accomplish this, the exit function is used.

Function Prototypes also known as function declarations .

A function prototype eliminates the need to place a function definition before all calls to the function.

A local variable is defined inside a function and is not accessible outside the function.

A function's local variables exist only while the function is executing. Any value stored in a local variable is lost between calls to the function.

Global Constants Try to avoid the use of global variables, it is generally permissible to use global constants in a program.

A global constant is a named constant that is available to every function in a program. A global constant's value cannot be changed during the program's execution

A global variable is defined outside all functions and is accessible to all functions in its scope. Unless you explicitly initialize numeric global variables, they are automatically initialized to zero. Global character variables are initialized to NULL. *

A global variable is defined BEFORE any functions In most cases, you should declare variables locally and pass them as arguments to the functions that need to access them. * The NULL character is stored as ASCII code 0

You cannot have two local variables with the same name in the same function. This applies to parameter variables as well.

A parameter variable is, in essence, a local variable. So, you cannot give a parameter variable and a local variable in the same function the same name.

When passing a variable as an argument during a function call simply write the variable name inside the parentheses of the function call. Do not write the data type of the argument variable in the function call. Any expression whose value could normally be assigned to num may be used as an argument.

Correct: displayValue(x); Incorrect; displayValue(int x); Correct: displayValue(5 + 6) // This would pass 11

Default arguments are passed to parameters automatically if no argument is provided in the function call. In both example prototypes, the function showArea has two double parameters. Because both parameters have default arguments, they may optionally be omitted in the function call showArea(); One or both defaults can be overridden by providing different arguments when the function is called. If a function does not have a prototype, default arguments may be specified in the function header. The showArea function could be defined as follows: >> When an argument is left out of a function call, all arguments that come after it must be left out as well.

Default arguments are literal values or constants with an = operator in front of them, appearing after the data types listed in a function prototype: void showArea(double = 20.0, double = 10.0); OR showArea(double length = 20.0, double width = 10.0); The default arguments are only used when the actual arguments are omitted from the function call. showArea(12.0); //will pass 12 as the length #################### void showArea(double length = 20.0, double width = 10.0) A function's default arguments should be assigned in the earliest occurrence of the function name.

void Functions do not return a value to the part of the program that executed (called) it. void displayMessage() { cout << "Hello from the function displayMessage.\n"; }

For example, a function that simply prints a message may not need to return any value.

Global variables make debugging difficult. Any statement in a program can change the value of a global variable. If you find that the wrong value is being stored in a global variable, you have to track down every statement that accesses it to determine where the bad value is coming from.

Global variables make a program hard to understand. A global variable can be modified by any statement in the program. If you are to understand any part of the program that uses a global variable, you have to be aware of all the other parts of the program that access the global variable.

A driver is a program that is designed to call and test a function. This allows the function to be tested in isolation from the rest of the program it will eventually be a part of. p.362 6-30

If the function returns a value, the driver displays the return value on the screen. If the function performs as desired, it can be placed into the actual program it will be part of.

A function is a collection of statements that performs a specific task. Functions are commonly used to break a problem down into small manageable pieces. A function call is a statement that causes a function to execute. A function definition contains the statements that make up the function.

Instead of writing one long function that contains all of the statements necessary to solve a problem, several small functions that each solve a specific part of the problem can be written.

Parameters are special-purpose variables that are defined inside the parentheses of a function definition. Normally, when a parameter's value is changed inside a function, it has no effect on the original argument.

They are separate and distinct from the arguments that are listed inside the parentheses of a function call. The values that are stored in the parameter variables are COPIES of the arguments.

The exit function takes an integer argument exit(0); If you are unsure which code to use with the exit function, there are two named constants: EXIT_FAILURE = 1 EXIT_SUCCESS = 0 defined in cstdlib for you to use.

This argument is the exit code you wish the program to pass back to the computer's operating system: Process returned 0 (0x0) = Hexadecimal value The exit code zero is passed, which commonly indicates a successful exit. This code is sometimes used outside of the program to indicate whether the program ended successfully or as the result of a failure.

Functions that have multiple reference variables will require an ampersand before each reference variable name. Reference variables should ONLY be used as parameters when the situation requires them.

This function has 4 reference variables void addThree(int &, int &, int &, int &);

When you are writing a value-returning function, you must decide what type of value the function will return.

This is because you must specify the data type of the return value in the function header, and in the function prototype.

a value-returning function returns a value of a specific data type. You can use the function's return value anywhere that you can use a regular value of the same data type.

This means that anywhere an int value can be used, a call to an int value-returning function can be used. Likewise, anywhere a double value can be used, a call to a double value-returning function can be used. The same is true for all other data types.

Sending Data into a Function A parameter is a special variable that holds a value being passed into a function. Like all variables, parameters have a scope. The scope of a parameter is limited to the body of the function that uses it.

Values that are sent into a function are called arguments. variable definition inside the parentheses: void displayValue(int num) the variable num is a parameter. This enables the function to accept an integer value as an argument.

Passing Data by Value When only a copy of an argument is passed to a function, it is said to be passed by value .

When an argument is passed into a parameter, only a copy of the argument's value is passed. Changes to the parameter do not affect the original argument.

The return statement causes a function to end immediately.

When the return statement is encountered, the function immediately terminates and control of the program returns to the statement that called the function.

Using Reference Variables as Parameters reference variable that, when used as a function parameter, allows access to the original argument. a reference variable, it can manipulate the variable that was passed to the function as an argument.

When used as parameters, reference variables allow a function to access the parameter's original argument. Changes to the parameter are also made to the argument. This is called PASSING BY REFERENCE

You can have a local variable or a parameter variable with the same name as a global variable, or a global constant. overrides = shadows

When you do, the name of the local or parameter variable overrides the name of the global variable or global constant locally.

Functions may return true or false values a function can return a bool value. The following function accepts an int argument and returns true if the argument is within the range of 1 through 100, or false otherwise.

bool isValid(int number) { bool status; if (number >= 1 && number <= 100) status = true; else status = false; return status; }

Parameter variables are the arguments inside of the parenthesis of a function

function(parameter variables);

value-returning functions p.324 These functions may send a value back to the part of the program that called the function.

several arguments may be passed into a function, only one value may be returned from it. However, it is possible to return multiple values from a function, but they must be "packaged" in such a way that they are treated as a single value. This is a topic of Chapter 11.

Reference variables are defined like regular variables, except you place an ampersand (&) in front of the name. void doubleNum(int &); // This declare this function has an un-named reference variable. The ampersand must appear in both the prototype and the header of any function that uses a reference variable as a parameter. It does NOT appear in the function call.

void doubleNum(int &refVar) { refVar *= 2; } ONLY variables may be passed by reference. If you attempt to pass a nonvariable argument, such as a literal, a constant, or an expression, into a reference parameter, an error will result.

A stub is a dummy function that is called instead of the actual function it represents. It usually displays a test message acknowledging that it was called, and nothing more. This is an example of a stub. >> A stub allows you to determine whether your program is calling a function when you expect it to.

{ cout << "The showFees function was called with " << "the following arguments:\n" << "memberRate: " << memberRate << endl << "months: " << months << endl; }

When creating a function, you must write its definition. All function definitions have the following parts: ∙ Return type: ∙ Name: ∙ Parameter list: ∙ Body: This line int main() is the function header. The variables inside the () is the parameter list: void someFunction(string str, int pos) Function main is called automatically when a program starts, but all other functions must be executed by function call statements. If data is not being passed into the function being called the parentheses are left empty. readThis(); This calls the readThis function.

Return type: A function can send a value to the part of the program that executed it. The return type is the data type of the value that is sent from the function. ################################ Name: You should give each function a descriptive name. In general, the same rules that apply to variable names also apply to function names. ################################ Parameter list: The program can send data into a function. The parameter list is a list of variables that hold the values being passed to the function. ################################ Body: The body of a function is the set of statements that perform the function's operation. They are enclosed in a set of braces.

Overloading Functions p.354 6-27 Two or more functions may have the same name, as long as their parameter lists are different. In C++, each function has a signature. The function signature is the name of the function and the data types of the function's parameters in the proper order. You can't have identical functions in the same program. They must have different parameters. Either types of, or numbers of parameters.

Sometimes you will create two or more functions that perform the same operation, but use a different set of parameters or parameters of DIFFERENT DATA TYPES. The functions in Program 6-27 would have these signatures: square(int) square(double) When an overloaded function is called, C++ uses the function signature to distinguish it from other functions with the same name.

Static Local Variables p.343 6-22 void showStatic() { static int statNum; // This declares a static int cout << "statNum is " << statNum << endl; statNum++; }

Static local variables are not destroyed when a function returns. They exist for the lifetime of the program, even though their scope is only the function in which they are defined. Static local variable are only initialize once.

value-returning function will use int , double , bool , or any other valid data type in its header. Variables that are defined inside a function are called local variables. Note: You can call a VRF as part of an expression average = sum(x, y) / 2.0; average = (return value) / 2

The return type of this function named sum, is int. int sum(int num1, int num2) { int result; result = num1 + num2; return result; } The return result statement causes the function to end, and it sends the value of the result variable back to the statement that called the function.


Related study sets

NSG 100 Exam #1 Review Questions

View Set

Science Year 10- Acids and Bases

View Set

Chapter 5: Perceiving Objects and Scenes

View Set

Central Venous Access Devices and Management

View Set