Chapter 6 - Functions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

6.13 What are reference variables?

When used as parameters, reference variables allow a function to access the parameters original argument. (p. 350)

6.8 When writing a value-returning type function...

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

6.10 Can you initialize local variables with function parameters?

Yes. (p. 338)

6.15 exit function will unconditionally...

... Shut down your program. Because it bypasses a programs logical flow, you should use it with caution. (p. 362)

6.11 When the function terminates, the local variables...

... the local variables are destroyed when the function terminates and are recreated when the function starts again. (p. 344)

6.12 When a function has a mixture of parameters, both with and without default arguments...

... the parameters with default arguments must be declared last. (p. 350)

6.11 Static local variables are initialized to...

... zero by default. (p. 348)

6.10 What are global constants?

A global constant is a named constant that is available to every function in the program. The value stored in global constants cannot be changed. (p. 340)

6.16 What is a stub?

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.

6.4 Define arguments.

Arguments are values which are sent into functions. Also known and passed into functions.

6.8 Define value-returning functions.

Data may be passed into a function by way of parameter variables. Data may also be returned from a function, back to the statement that followed it.

6.12 Explain default arguments.

Default arguments are passed to parameters automatically if no argument is provided in the function call. A default parameter is passed to the parameter when the actual parameter is left out of the function call. (p. 347)

6.1 What is divide and conquer?

Divide and conquer is a term used to describe the act of writing several small functions and combining them, to complete a program. Large projects will utilize this technique. A large problem is divided into several smaller problems that are easily solved.

6.13 Reference variables should only be used...

Don't get carried away with using reference variables as function parameters. Any time you allow a function to alter a variable that's potential bugging problems. Reference variables should only be used as parameters when the situation requires them. (p. 354)

6.9 Functions may return..

Functions may return a true or false value (boolean)

6.8 It is possible to return multiple values from a function...

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.

6.4 The scope of a parameter is limited to the...

Like all variables, parameters have a scope. The scope of a parameter is limited to the body of the function that uses it.

6.4 While writing a function prototype, is it necessary to list the name of the parameter variable, within the parenthesis?

No! Only its data type is required. The compiler will ignore the name of the parameter variable.

6.13 You may only pass ________ by reference.

Only variables may be passed by reference. If you attempt to pass a non-variable argument, such as a literal, a constant or an expression into a reference parameter, an error will result. (p. 354)

6.10 True or False and eloaborate. You cannot have two local variables with the same name in the same function.

TRUE. This applies to parameter variables as well. A parameter variable is, in essence, a local variable so you cannot give a local variable and parameter the same name either. You can have a local variable or parameter with the same name as a global variable or global constant. (p. 343)

6.15 What does EXIT_SUCCESS mean?

The constant EXIT_SUCCESS is defined as the termination of code that commonly represents a successful exit under the current operating system. exit(EXIT_SUCCESS); (p. 361)

6.15 Define the exit() function

The exit function causes a program to terminate regardless of which function or control mechanism is executing. (p. 360)

6.14 Define function signature

The function signature is the name of the function and the data types of the functions parameters in the proper order. C+ uses the function signature to differentiate between overloaded functions. (p. 357)

6.7 The return statement causes...

The return statement causes a function to end immediately

6.15 Which header file is needed in order to use the exit() function?

To use the exit function you must include the <cstdlib> header file. (p. 361)

6.2 Describe what happens when you call a function.

A function is executed when it is called. Function main is called automatically when a program starts but all other functions must be executed by function call statements. When a function is called the program branches to that function and executes the statements in its body.

6.8 A function may send...

A function may send a value back to the part of the program that called the function.

6.3 What is a function prototype?

A function prototype eliminates the need to place a function definition before all calls to the function. The function prototype looks similar to the function header except that there is a semicolon at the end (a return type is indicated) void displayMessage();

6.12 When should default arguments be assigned?

A functions default arguments should be assigned in the earliest occurrence of the function name. This will usually be in the function prototype. (p. 348)

6.10 Explain the lifetime of a local variable

A functions local variables exist only when the function is executing. This is known as the lifetime of a local variable. (p. 338)

6.10 Define local and global variables

A local variable is defined inside a function and is not accessible outside the function. They are function specific. Other functions may have variables with identical names. A global variable is defined outside all functions and is accessible to all functions in its scope.

6.6 What is a modular program?

A modular program is broken up into functions that perform specific tasks.

6.4 What is a parameter? How would you use one in a function?

A parameter is a special variable that holds a value being passed into a function. By using parameters you can design your own functions. void pow(int x, int y) <---- Generic function example

6.4 What must each parameter variable in a parameter list have before its name?

Each parameter variable in a parameter list must have a data type listed before its name. void showSum(int num1, int num2, int num2);

6.15 Discuss exit codes.

Exit code zero is commonly used to indicate a successful exit. If you are unsure which code to use with the exit function there are two named constants, EXIT_FAILURE and EXIT_SUCCESS defined in <cstdlib> for you to use.

6.13 Define "passed by reference"

Function arguments are normally passed by value, which means a copy of the arguments value is passed into the parameter variable. When a reference parameter is used, it is said that the argument is passed by reference. (p. 352)

6.3 What are function prototypes also known as?

Function declarations

6.6 What are functions ideal for?

Functions are ideal for use in menu driven programs. When the user selects an item from a menu the program can call the appropriate functions.

6.10 What are some reasons not to make all your variables global?

Global variables make debugging difficult. If a variable is misbehaving you will have to look through each instance. Functions that use global variables are usually dependent on those variables. If you want to use the function in another program, you will have to redesign it to not be reliant on the global variable. Global variables make a program hard to understand. (p. 340)

6.10 Global variables should be defined...

Global variables should be defined before any functions. They can be declared after prototypes. (p. 338)

6.4 What happens when you pass an argument whose type is not the same as the parameters data type?

If you pass an argument with a different data type than the parameter, the argument will be promoted or demoted automatically. displayValue(int); displayValue(4.7); == displayValue(4) // 4 is passed (the decimal is removed)

6.4 What are the different ways to describe arguments and parameters?

Some people call: Arguments Parameters "Actual parameters" "Formal parameters" or "Actual arguments" "Formal arguments" Whichever way you choose, it is important to be consistent in your decisions.

6.11 Define static local variables

Static local variables are not destroyed when a function returns. They exist for the scope of the program though the scope is limited to the parent function. (p. 344-345)

6.10 Global variables are automatically set to...

zero (NULL)

6.12 (Regarding default arguments) When an argument is left out of a function call,

... all arguments which come after it must be left out as well. (p. 349)

6.12 When an argument is left out of a function call (because it has a default value)...

... all the arguments which come after it must be left out too. (p. 350)

6.13 (Reference Variables) 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. (p. 351)

6.12 If a function does not have a prototype, default arguments...

... may be specified in the functi0n header. (p. 348)

6.12 The value of a default argument must be...

... must be a literal value or a named constant. (p. 350)

6.15 Generally, the exit code is important only if...

... only if you know it will be tested outside of the program. It is is not used, just pass zero, or EXIT_SUCCESS. (p. 361)

6.4 When passing a variable as an argument...

... simply write the variable name inside the parenthesis of the function call. DO NOT write the data type of the argument variable in the function call. displayValue(int x); // Error! displayValue(x); // Correct!

6.16 The stub allows you to...

... 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. (p. 363)

6.16 What is/are driver(s)?

A driver is a program that tests a function by simply calling it. If the function returns a value. the driver displays a return value on screen. This allows you to see how the function performs in isolation from the rest of the program it will eventually be a part of. (p. 364)

6.2 What is a function call and what does a function definition contain?

A function call is a a statement that causes a function to execute. A function definition contains the statements which make up the function.

6.1 What is a function?

A function is a collection of statements that perform a specific task. Functions simplify programs. (p. 301)

6.15 What is EXIT_FAILURE?

EXIT_FAILURE (contained in <cstdlib>) is defined as the termination of code that commonly represents an unsuccessful exit under the current operating system. exit(EXIT_FAILURE); (p. 361)

6.13 How do you format reference variables?

Reference variables are defined like regular variables except you place an argument in front of the name. void doubleNum(int&refVar) // The ampersand is the argument placed in front of reference variable refVar // Can also be written as: void doubleNum(int&) (p. 351)

6.2 List and define the parts of a function definition

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 into the function. Body: The body of a function is the set of statements that perform the functions operation. They are enclosed in a set of braces.

6.5 What does "passed by value", mean?

When only a copy of an argument is passed to a function, it is said to be passed by value. The function receives a copy of the arguments value and does not have access to the original value.

6.2 What is a function header?

The function header is part of the function definition. It declares the functions return type, name and parameter list. It is not terminated with a semicolon because the definition of the functions body follows it. void displayMessage() <---- Function header displayMessage(); <---- Function call (notice the semicolon)

6.4 The function prototype must list the ...

The function prototype must list the data type of each parameter.

6.14 Define overloading functions.

Two or more functions may have the same name, as long as their parameter lists are different. (p. 356)

6.8 Describe value returning functions.

Value returning functions return a value of a specific type. You can use the functions return value anywhere that you can use a regular value of the same data type. The same is true for all data types.

6.8 Define local variables

Variables that are defined inside a function are called local variables.

6.2 What is a void function?

Void is a return type which indicates to the compiler that no return data is necessary. The function does not return a value to the part of a program that executed it.

6.4 When a function is called, what might a program do to it?

When a function is called, a program may send values into the function.

6.5 When an argument is passed into a parameter...

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


Set pelajaran terkait

Module 4: Chapter 11: Families and Society

View Set

Exam Three silvestri practice questions

View Set

OPM 200 Final Exam Study Guide Multiple Choice

View Set

Persuasion: Content Academic Vocabulary

View Set