CH6

Ace your homework & exams now with Quizwiz!

20. Unless you explicitly initialize global variables, they are automatically initialized to _________.

0 NOTE: Global char variables are initialized to NULL. The NULL character is stored as ASCII code o

9. Give an example where passing an argument by reference would be useful.

A function such as the following could be written to get user input. The input is stored in the variables that are passed as arguments. void getValues(int &x, int &y) { cout << "Enter a number: "; cin >> x; cout << "Enter another number: "; cin >> y; }

2. What is the difference between an argument and a parameter variable?

Argument: are values that are passed/sent into a function. ex: function displayValue() is being called and the argument ,5, is passed into it: displayValue(5); Parameter: Is a special variable that holds a value being passed into a function. ex: void displayValue(int num) { cout << "The value is " << num << endl; } Note: the integer variable definition inside the parentheses (int num). The variable num is a parameter. It enables the function displaValue to accept an interger value as an argument. It's not necessary to list the parameter variable inside the parentheses only the DATA TYPE is required. Ex: void displayValue(int)

.1. Why do local variables lose their values between calls to the function in which they are defined?

Because they are created in memory when the function begins execution, and are destroyed when the function ends.

12. Either a function's _________ or its _________ must precede all calls to the function.

Definition prototype

54. T F The exit function can only be called from main .

F The exit() function cause a program to terminate, regardless of which function or control mechanism is executing. p358

42. T F When a function terminates, it always branches back to main , regardless of where it was called from.

F p303-305 shows the way a function would normally branch back to main; however, the exit function will terminate a function regardless of which function contains the call. p358

50. T F All static local variables are initialized to −1 by default.

F Like global variable, all static local variables are initialized to zero by default. p343

45. T F Changes to a function parameter always affect the original argument as well.

F Normally when a Parameter's value is changed inside a function, it has no effect on the original argument. p316. An exception: using reference variables as parameters. p348

39. T F Function headers are terminated with a semicolon.

F p302 Note: below are terminated w/ a semicolon -function call (return type not listed) p303 -function prototype, aka function declaration, (no body {})p309

53. T F It is not possible for a function to have some parameters with default arguments and some without.

Flase Note: a function can have some parameters w/ default arguments and some w/o. The default arguments need to be defined last. ex: void calcPay(int empNum, double payRate, double hours = 40.0); p348

3. Where do you define parameter variables?

Inside the parentheses of a function header.

8. How would a static local variable be useful?

It maintains the state of a variable between function calls, whereas local variables do not persist between function calls. local variables are destroyed when the function terminates and are re-created when the function starts again.

7. What is the advantage of breaking your application's code into several small procedures?

It makes the program easier to manage. Imagine a book that has a thousand pages, but isn't divided into chapters or sections. Trying to find a single topic in the book would be very difficult. Real-world programs can easily have thousands of lines of code, and unless they are modularized, they can be very difficult to modify and maintain.

13. Values that are sent into a function are called _________.

Arguments

25. When a function uses a mixture of parameters with and without default arguments, the parameters with default arguments must be defined _________.

last ex: void calcPay (int empNum, double payRate, double hours = 40.0) //illegal prototype void calcPay (int empNum, double hours = 40.0, double payRate) p348

27. When used as parameters, _________ variables allow a function to access the parameter's original argument.

reference

29. Reference variables allow arguments to be passed by ____________.

reference

23. The _________ statement causes a function to end immediately.

return

15. When only a copy of an argument is passed to a function, it is said to be passed by _________.

value

.11. If a function doesn't return a value, the word _________ will appear as its return type.

void

5. When a function accepts multiple arguments, does it matter in what order the arguments are passed?

Yes. The first argument is passed into the parameter variable that appears first inside the function header's parentheses. Likewise, the second argument is passed into the second parameter, and so on.

6. How do you return a value from a function?

You must first specify the data type of the return value in the function prototype and function header such as int, double, bool, etc. EX: int sum (int num1, int num2) { int result; result = num1 + num2; return result; } The lasts statement in the function is return result which causes the function to end and sends the value of result back to the statement that called the function.

28. Reference variables are defined like regular variables, except there is a(n) _________ in front of the name.

ampersand &

24. _________ arguments are passed to parameters automatically if no argument is provided in the function call.

default Note: default arguments are usually listed in the function prototype. EX: void showArea(double = 20.0, double = 10.0); default arguments are literal values or constants w/ an = operator in front of them. EX: showing above with optional parameter names included. (parameter names are not required in function prototypes. void showArea(double length = 20.0, double width = 10.0)

30. The _________ function causes a program to terminate.

exit() p358

10. The _________ is the part of a function definition that shows the function name, return type, and parameter list.

function header

16. A(n) _________ eliminates the need to place a function definition before all calls to the function.

function prototype

18. _________ variables are defined outside all functions and are accessible to any function within their scope.

global

19. _________ variables provide an easy way to share large amounts of data among all the functions in a program.

global

26. The value of a default argument must be a(n) _________.

literal value or named constant. (additional point about default arguments p348): -when an argument is left out of a function call (because it has a default value), all the arguments that come after must be left out too. -When a function has a mix of parameters (w/ & w/o arguments), the parameters w/ default arguments must be declared last.

17. A(n) _________ variable is defined inside a function and is not accessible outside the function.

local

21. If a function has a local variable with the same name as a global variable, only the _________ variable can be seen by the function.

local

31. Two or more functions may have the same name, as long as their _________ are different.

parameter lists overloading functions p354

14. Special variables that hold copies of function arguments are called _________.

parameters p311

4. If you are writing a function that accepts an argument and you want to make sure the function cannot change the value of the argument, what do you do?

pass the data by VALUE so that only a copy of the argument is passed to a function. This way the function does not have access to the original argument like it would if you used a pass by REFERENCE. Pass by value does not use the ampersand & . Below are examples of pass by reference: function prototype: void doubleNum(int &); function definition: void doubleNum(int &someVariable) { statement; }

22. _________ local variables retain their value between function calls.

static

49. T F Static local variables are not destroyed when a function returns.

T

51. T F Initialization of static local variables only happens once, regardless of how many times the function in which they are defined is called.

T This is because initialization normally happens when the variable is created , and static local variables are only created once during the running of the program. P343

52. T F When a function with default arguments is called and an argument is left out, all arguments that come after it must be left out as well.

T ex: displayStars (7, 3); displayStrars ( , 3); //illegal function call pg347

43. T F Arguments are passed to the function parameters in the order they appear in the function call.

T p315

48. T F Overuse of global variables can lead to problems.

T -global variables make debugging difficult. -Functions that use global variables are usually dependent on those variables. -Global variable make a program hard to understand. p338

46. T F In a function prototype, the names of the parameter variables may be left out.

T Only its data type is required, however both are acceptable.: void displayValue (int); vs (int num); p312

44. T F The scope of a parameter is limited to the function which uses it.

T The scope of a parameter is limited to the BODY of the function that it uses. p315

47. T F Many functions may have local variables with the same name.

T because local variables defined inside a function are local to that function, the function is hidden from the statements in other functions; therefore, other functions may have separate, distinct variables with the same name. p334

38. T F Functions should be given names that reflect their purpose.

T p301 same rules for naming variables

41. T F If other functions are defined before main , the program still starts executing at function main .

T p302 & 303

40. T F Function prototypes are terminated with a semicolon.

T p309

55. T F A stub is a dummy function that is called instead of the actual function it represents.

T p361 A driver is a program that tests a function by simply calling it.


Related study sets

Chapter 62: Managements of Patients with Burn Injury (Brunner)

View Set

Fizika II - instant bukó kérdések

View Set

Chapter 47: Management of Patients With Gastric and Duodenal Disorders

View Set

Jason Dion's CySA+ Practice Exam 1

View Set

Chapter 20: Cell Communities: Tissues, Stem Cells and Cancer

View Set

Chapter 10: Supply Chain Security

View Set

Living Environment - Multiple Choice

View Set

Common Skin Conditions (unfinished)

View Set

66 Books of the Bible (5,12,5,5,12)

View Set

American Constitution and Government

View Set