Chapter 6

Ace your homework & exams now with Quizwiz!

Static Local Variable

-"Remember" what value is stored in a local variable between function calls -Not destroyed when a function returns; exist for the lifetime of the program -Initialized to zero by default; if initialization is provided, the initialization only occurs once -Place static in front of variable defintion ex. static int num;

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

Function call

-A function is executed when it is called -All other functions besides main must be executed by function call statements -When a function is called, the program branches to that function and executes the statement in its body -Name of the function, followed by a set of parentheses and a semicolon

Function Protoype

-A method to ensure the compiler knows the function's return type, parameters, and type of each parameter function type, name, (); ex. void displayMessage (); -Also known as function declarations -Must place before all calls to the function; can be placed outside of main function -Must list the data type of each parameter

Global Constant

-A named constant that is available to every function in the program -Can have same name as local variable or parameter variable

Driver

-A program that tests a function by calling it -If the function accepts arguments, the driver passes test data -If the function returns a value, the driver displays the return value on the screen

Global Variable

-Any variable defined outside all the functions in a program -Can be accessed by all functions that are defined after the global variable is defined -Unless you explicitly initialize numeric global variables, they are automatically initialized to zero (NULL) -Can have same name as local variable or parameter variable

Modular Program

-Broken up into functions that perform specific tasks

Calling a Value-Returning Function

-Can call by assigning to a variable ex. total = sum(value1, value2); -Can show a mathematical expression that uses a call to the function ex. average = sum(x, y) / 2.0; -You can use the function's return value anywhere that you can use a regular value of the same data type, example; anywhere an int value can be used, a call to an int value-returning function can be used

Parameter

-Can design functions that accept data as arguments -Special variable that holds a value being passed into a function -ex. void displayValue(int num) The variable num is a parameter -Also called formal parameters -Not necessary to list the name of the parameter variable inside the parentheses of prototype; only its data type is required -If you pass an argument whose type is not the same as the parameter's type, the argument will be promoted or demoted automatically -Each parameter variable in a parameter list must have a data type listed before its name ex. void showSum(int num1, int num2, int num3) -The scope of a parameter is limited to the body of the function that uses it

Return statement

-Causes a function to end immediately ex. if (arg2 == 0.0) { cout << "Sorry\n"; return; }

exit () Function

-Causes a program to terminate, regardless of which function or control mechanism is executing -Must include cstdlib header file

Value-Returning Functions

-Data may also be returned from a function, back to the statement that called it; these are functions that return a value -Only one value may be returned from these functions -Must decide what type of value the function will return ex. int sum(int num1, num2) Returns an int value -Must have a return statement in this format: return expression; -expression is the value to be returned; can be a variable, literal, or mathematical expression; value of the expression is converted to the data type that the function returns

Local variables

-Defined inside a function -Hidden from statements in other functions -Exist only when the function is executing; known as the lifetime of a local variable -Any value stored in a local variable is lost between calls to the function in which the variable is declared -Cannot give a parameter variable and a local variable in the same function the same name

Stub

-Dummy function that is called instead of the actual function it represents -Usually displays a test message acknowledging that it was called, and nothing more -Allows you to determine whether your program is calling a function when you expect it to, and to confirm that valid values are being passed to the function

Function Name

-Each function should have a descriptive name -Same rules that apply to variable names also apply to function names

Function header

-First line when calling a function -Declares the functions return type, name, and parameter list -Not terminated with a semicolon

More than one function call

-If a function is called more than once in a program, the values stored in the function's local variables do not persist between function calls -Local variables are destroyed when the function terminates, and then re-created when the function starts again

Overloading Functions

-May assign the same name to multiple functions, as long as their parameter lists are different -Uses function signature to distinguish it from other functions with the same name

Function signature

-Name of the function and the data types of the function's parameters in the proper order -Return value is not part of the signature

Default Argument

-Passed to the parameter when the actual argument is left out of the function call -Usually listed in the function prototype -Literal values or constants with an = operator in front of them, appearing after the data types listed in a function prototype -If a function does not have a prototype, default arguments may be specified in the function header -Should be assigned in the earliest occurrence of the function name; usually the prototype -When an argument is left out of a function call, all arguments that come after it must be left out as well -Can have some parameters w/ default arguments and some w/o, but parameters w/ default arguments must be defined last

Void Functions

-Perform one or more statements, which follows terminate -Do not return a value

Reference Variable

-Special type of variable that, when used as a function parameter, allows access to the original argument -A function may change a variable that is defined in another function -Defined like regular variables, except have an & in front of the name -When prototyping a function with a reference variable, include the & after the data type, as well as in the function header; does not appear in the function call

Function definition

-Statements that makeup a function -When creating a function, you must write its definition -All function definitions have the return type, name, parameter list, and body -Must place before calls to the function

Body

-The body of a function is the set of statements that perform the function's operation -Enclosed in a set of braces

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

Arguments

-Values that are sent into a function -Listed inside the parentheses of a function call, then copied into the function's parameter variable -Do not write the data type of the argument variable in the function call -Also called actual parameters

Returning a Boolean Value

-When there is a need for a function that tests an argument and returns a true or false value, indicating whether or not a condition exists -Return a bool value, true or false


Related study sets

MSA622 EXAM 2 (CH 10) STUDY GUIDE

View Set

Ch. 21 Cooling System "Test" Auto Tech

View Set

Chapter 32- Skin Integrity and Wound Care

View Set