# C++ Workshop - Week 4 (Ch. 5) - Quizzes and Exercises

This topic is 3743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

# Quizzes and Extra Credit for Week 4.

Post your answers to the Quizzes from Week 4 here rather than in the chapter thread, so as not to spoil things for the others.
Chapter 5 Quiz 1. Where do global functions exist? Where do member functions exist? 2. What is a function? 3. When a function identifier is encountered, where does execution jump to? Where does it return to after the function terminates? 4. How do you declare a function’s return value type? 5. What is a parameter list? 6. Can you use a function before it has been declared? Can you use it before it has been defined? 7. If a function has been defined before being used, must I provide a separate function declaration (prototype)? 8. Is it wise to use definitions throughout your code in place of declarations? Why or why not? 9. Is it necessary that a function prototype and definition agree EXACTLY about return type and signature? 10. Must a function prototype includes the names of variables in the parameter list? 11. Must the parameter names in the declaration (prototype) match the parameter names in the definition? 12. What do we call variables created within the scope of a block? 13. What happens to those variables when the function returns to a higher scope? 14. [Extra Credit] Are these variables created on the stack, or the heap? 15. By default, are parameter variables local or global? What does this mean for the value of the variable after the function returns? 16. What do we call the default passing method which implies the creation of temporary variables? 17. What is a global variable? Where can it be accessed? 18. What happens to global variables when there is a local variable with the same name? 19. Why are global variables dangerous? 20. [Extra Credit] How short or long (how many lines) should a function be? (yes, there is a correct, but tricky answer) 21. What can be passed as input to a function? What is the only requirement? 22. How do you return a value from within a function? 23. What is a “default value” for a parameter value? Where is it specified? What does it look like? 24. Is this a valid prototype? What does it mean? “int MyIntFunction( int = 10, int = 20 );” 25. What is function overloading? What benefit does it provide? 26. What is recursion? When is it useful?

##### Share on other sites
1. Where do global functions exist? Where do member functions exist?
Outside of objects and classes. Within a class.

2. What is a function?
A sub-program that can act on data and return a result.

To the body of that function. Back to the line after the calling function.

4. How do you declare a function’s return value type?
Similar to declaring a variable, like int someFunction.

5. What is a parameter list?
The description of the values you send, int Area(int height, int width);

6. Can you use a function before it has been declared? Can you use it before it has been defined?
No and yes.

7. If a function has been defined before being used, must I provide a separate function declaration (prototype)?
No. It should act as its own prototype.

8. Is it wise to use definitions throughout your code in place of declarations? Why or why not?
No, it makes maintenance more difficult, may prevent you from calling functions if the order of definitions is incorrect, and can make debugging more difficult.

9. Is it necessary that a function prototype and definition agree EXACTLY about return type and signature?
Yes or you will get a compile-time error.

10. Must a function prototype include the names of variables in the parameter list?
No, but you should so as to make understanding the prototype easier.

11. Must the parameter names in the declaration (prototype) match the parameter names in the definition?
No, but it is a good idea to do so.

12. What do we call variables created within the scope of a block?
Local

13. What happens to those variables when the function returns to a higher scope?
‘Marked for destruction’

14. [Extra Credit] Are these variables created on the stack, or the heap?
On the stack.

15. By default, are parameter variables local or global? What does this mean for the value of the variable after the function returns?
Local. It does not pass through beyond the execution of that function.

16. What do we call the default passing method which implies the creation of temporary variables?
Passing by value.

17. What is a global variable? Where can it be accessed?
A variable defined outside any function. It can be accessed inside any function.

18. What happens to global variables when there is a local variable with the same name?
It is hidden.

19. Why are global variables dangerous?
The data is shared throughout the entire program, can be changed anywhere and will end up being very hard to debug.

20. [Extra Credit] How short or long (how many lines) should a function be? (yes, there is a correct, but tricky answer)
5-15, should be able to be viewed in a single screen without scrolling.

21. What can be passed as input to a function? What is the only requirement?
Any valid expression. It must match the argument type that is expected.

22. How do you return a value from within a function?
Write ‘return’ followed by the value you want returned.

23. What is a “default value” for a parameter value? Where is it specified? What does it look like?
The value used if no value is supplied. It is specified in the declaration. Int Area (int = 10);

24. Is this a valid prototype? What does it mean? “int MyIntFunction( int = 10, int = 20 );”
Yes, if no value is supplied for either of the parameters the first is assigned a value of 10 and the second parameter is assigned a value of 20.

Creating more than one function with the same name. Saves time by allowing you to call one function regardless of the input type.

26. What is recursion? When is it useful?
When a function calls itself. Apparently when doing a Fibonacci sequence, or when you want to repeatedly act on data in the same way.

##### Share on other sites
1. Where do global functions exist? Where do member functions exist?
Global functions are outside classes, member functions are in classes.

2. What is a function?
A piece of code that can be called and returns a result.

The execution jumps to start of the function, and returns to the line on which the function identifier was when the function terminates.

4. How do you declare a function’s return value type?
By putting the type name in front of the function name.

5. What is a parameter list?
The parameters that are given to a function. These are written between braces behind the function name.

6. Can you use a function before it has been declared? Can you use it before it has been defined?
No. Yes, if it has been declared.

7. If a function has been defined before being used, must I provide a separate function declaration (prototype)?
No.

8. Is it wise to use definitions throughout your code in place of declarations? Why or why not?
No. This makes it very hard to do maintenance on your programs. Functions can only call other functions if they have been either declared or are devined before they have been devined. If you only use devinitions, you have to keep a strict order in which the functions are devined.

9. Is it necessary that a function prototype and definition agree EXACTLY about return type and signature?
Yes.

10. Must a function prototype includes the names of variables in the parameter list?
No.

11. Must the parameter names in the declaration (prototype) match the parameter names in the definition?
No.

12. What do we call variables created within the scope of a block?
Scope or Local variables.

13. What happens to those variables when the function returns to a higher scope?
They removed from the stack.

14. [Extra Credit] Are these variables created on the stack, or the heap?
Stack.

15. By default, are parameter variables local or global? What does this mean for the value of the variable after the function returns?
Local. It doesn't exist anymore.

16. What do we call the default passing method which implies the creation of temporary variables?
Pass by value.

17. What is a global variable? Where can it be accessed?
A variable that is declared globally and can be accessed by any piece of code.

18. What happens to global variables when there is a local variable with the same name?
The local variable is used as long as a piece of code uses that name in the scope in which the local variable exists.

19. Why are global variables dangerous?
Because they can be changed by every piece of code, therefore it is very hard to keep track of which piece of code changed a global variable.

20. [Extra Credit] How short or long (how many lines) should a function be? (yes, there is a correct, but tricky answer)
Heh, this is the first question in this course I don't have answer for...

21. What can be passed as input to a function? What is the only requirement?
Any expression that matches the type in the function parameter list.

22. How do you return a value from within a function?
return value.

23. What is a “default value” for a parameter value? Where is it specified? What does it look like?
A value that is used when no value is passed in. Its specified in the declaration and looks like:
int function(int parameter = 10);

24. Is this a valid prototype? What does it mean? “int MyIntFunction( int = 10, int = 20 );”
Yes. It declares a function called MyIntFunction that returns an int, has two int parameters which have default values 10 and 20.

Creating a function with the same name as an other function but with a different parameters list. This enables you to give the same name to functions that do the same, but have different parameter types.

26. What is recursion? When is it useful?
Calling a function from the function itself. Its usefull when you want to process a hierachy of things, like processing a tree or xml.

##### Share on other sites
1. Where do global functions exist? Where do member functions exist?
Global functions exist everywhere in a program and member functions exist only within a class (Although this is covered in chapter 6, not 5, correct?)

2. What is a function?
A function is a subprogram that acts on data and returns a value.

Execute jumps to the beginning of that function. When that function termintes, it returns to the line after where the original function was called.

4. How do you declare a function’s return value type?
By placing the return type value before the function's name. IE:
int myFunction (int x);

5. What is a parameter list?
A list of variables fed to a function for it to use in its processing.

6. Can you use a function before it has been declared? Can you use it before it has been defined?
No and yes.

7. If a function has been defined before being used, must I provide a separate function declaration (prototype)?
No.

8. Is it wise to use definitions throughout your code in place of declarations? Why or why not?
No, it isn't good programming to have your definitions in a 'random' order throughout your program, it's sloppy. Also, prototypes are quite useful in debugging when it comes to checking variable type integrity.

9. Is it necessary that a function prototype and definition agree EXACTLY about return type and signature?
Yes absolutely.

10. Must a function prototype includes the names of variables in the parameter list?
No, although it is reccomended for clarity sake.

11. Must the parameter names in the declaration (prototype) match the parameter names in the definition?
No.

12. What do we call variables created within the scope of a block?
Local variables.

13. What happens to those variables when the function returns to a higher scope?
They are set for destruction by the compiler.

14. [Extra Credit] Are these variables created on the stack, or the heap?
The stack.

15. By default, are parameter variables local or global? What does this mean for the value of the variable after the function returns?
They are local, which means that any modifications made to them in your function will be 'undone' when processing leaves that function.

16. What do we call the default passing method which implies the creation of temporary variables?
Passing by value.

17. What is a global variable? Where can it be accessed?
A global variable is created outside of any function and can be accessed by any function of the program.

18. What happens to global variables when there is a local variable with the same name?
The local variable is used and temporarily 'covers up' the global variable.

19. Why are global variables dangerous?
When any function can change a variable's value, it makes debugging a program much more difficult.

20. [Extra Credit] How short or long (how many lines) should a function be? (yes, there is a correct, but tricky answer)
As short as possibe, in an ideal world, it should be able to fit on a screen.

21. What can be passed as input to a function? What is the only requirement?
Any valid C++ expression can be passed into a function as an argument, as long as that expression matches the variable type specified in the definition.

22. How do you return a value from within a function?
return x;

23. What is a “default value” for a parameter value? Where is it specified? What does it look like?
For example, if I have:
int myFunction (int x=50);
50 is the default value for x. If I pass no value for x into the function when I call it, 50 will be used.

24. Is this a valid prototype? What does it mean? “int MyIntFunction( int = 10, int = 20 );”
Absolutely. This is a function called MyIntFunction that returns an int value. It also takes in two int values when it is called. If no values are given to one or none of these two values, it uses 10 and 20, respectively.

Function overloading allows you to use the same function name over and over with different lists of paremeters being passed in. The pgm then uses the correct function, depending on the types of parameters passed in. This makes for easier to understand function names, much cleaner in general.

26. What is recursion? When is it useful?
Recursion is the act of a function calling itself, and is useful when you need the same type of action performed on a set of data over and over again.

I am looking forward to the upcoming workshop!!!!!

##### Share on other sites

1. Where do global functions exist? Where do member functions exist?
1A. Global functions exist outside of objects and classes. Member functions exist within a class.

2. What is a function?
2A. A function is a sub-program that can act on input or global data, perform calculations or call other functions, and return a value.

3A. The body of the function being called. It returns to the beginning of the next statement in the calling function.

4. How do you declare a function’s return value type?
4A. By placing the type name before the function name in the declaration and definition. Ie. int MyFunction();

5. What is a parameter list?
5A. The list of values being passed as input into a function.

6. Can you use a function before it has been declared? Can you use it before it has been defined?
6A. No. You must declare it first. Yes, you can use it before it has been defined. This is referred to as using the “function prototype.” The only requirement is that the linker be able to find the definition and link-time.

7. If a function has been defined before being used, must I provide a separate function declaration (prototype)?
7A. No. The definition acts as its own prototype.

8. Is it wise to use definitions throughout your code in place of declarations? Why or why not?
8A. No. It is best to put your definition in a logical source file, and then spatter declarations around your code whenever the function is needed. This prevents circular logic and situations in which the order you include your header files matters.

9. Is it necessary that a function prototype and definition agree EXACTLY about return type and signature?
9A. Yes. They must match exactly in order for the prototype to be accurately mapped to its definition.

10. Must a function prototype includes the names of variables in the parameter list?
10A. No. The type is all that is necessary, however providing variables names makes the code more readable and the functions easier to use.

11. Must the parameter names in the declaration (prototype) match the parameter names in the definition?
11A. No. The variable names in the prototype are ignored.

12. What do we call variables created within the scope of a block?
12A. Local variables.

13. What happens to those variables when the function returns to a higher scope?
13A. The variables are no longer available, and are marked for destruction by the compiler.

14. [Extra Credit] Are these variables created on the stack, or the heap?
14A. The stack. Hence, where we are on the call stack determines the location of the variable stack.

15. By default, are parameter variables local or global? What does this mean for the value of the variable after the function returns?
15A. By default the parameters are local. This means they are destroyed like any local variable after the function terminates.

16. What do we call the default passing method which implies the creation of temporary variables?
16A. Pass-by-value

17. What is a global variable? Where can it be accessed?
17A. Variables created outside of the scope of any function – including main. They can be accessed from anywhere.

18. What happens to global variables when there is a local variable with the same name?
18A. It is temporarily hidden by the local variable for the scope of the local variable.

19. Why are global variables dangerous?
19A. Because they are shared data which can be modified from anywhere in the program, making it difficult to track down where the value might be changing and thus creating difficult to find bugs.

20. [Extra Credit] How short or long (how many lines) should a function be? (yes, there is a correct, but tricky answer)
20A. As short or long as it needs to be to get the job done, while still being easy to read and understand.

21. What can be passed as input to a function? What is the only requirement?
21A. Any valid expression. The only requirement is the result of the expression matches the parameter type.

22. How do you return a value from within a function?
22A. By using the return keyword, with the value you want to return. Ie. return 20;

23. What is a “default value” for a parameter value? Where is it specified? What does it look like?
23A. A default value is the value to be used for a parameter if none is passed in. It is specified in the declaration/prototype. It looked like an equal sign with a value.

24. Is this a valid prototype? What does it mean? “int MyIntFunction( int = 10, int = 20 );”
24A. Yes it’s valid. It means I have a function which takes two integers, each with default values, that return an integer as the result.

25A. Creating more than 1 function with the same name, but with a different signature. It allows you to call a function with same name that behaves differently depending on input.

26. What is recursion? When is it useful?
26A. Recursion is when you have a function which calls itself, either directly, or indirectly through a chain of other function calls. It is useful when you have a problem which is best solved by performing the same calculation on the result of that calculation. It is important to note that any problem which can be solved with recursion can also be solved with iteration, which we’ll learn about later. Though sometimes the code is more elegant with recursion.

##### Share on other sites
1. Where do global functions exist? Where do member functions exist?
Global functions exist outside of classes and objects. Member functions exist within.

2. What is a function?
A small code program that can be called upon, data can be inputed and it will output somthing back.

The body of the function. It returns to the beginning of the next statement in the calling function

4. How do you declare a function’s return value type?
In the function prototype. float myfunction();

5. What is a parameter list?
A list of all the parameters and their types, separated by commas.

6. Can you use a function before it has been declared? Can you use it before it has been defined?
No, Yes

7. If a function has been defined before being used, must I provide a separate function declaration (prototype)?No

8. Is it wise to use definitions throughout your code in place of declarations? Why or why not?
No

9. Is it necessary that a function prototype and definition agree EXACTLY about return type and signature?
Yes, for the prototype to match the definition.

10. Must a function prototype includes the names of variables in the parameter list?
No, the type is the only thing that matters.

11. Must the parameter names in the declaration (prototype) match the parameter names in the definition?
No, prototype variable names in the parameter list dont matter.

12. What do we call variables created within the scope of a block?
Local variables

13. What happens to those variables when the function returns to a higher scope?
Not available anymore.

14. [Extra Credit] Are these variables created on the stack, or the heap?
Stack

15. By default, are parameter variables local or global? What does this mean for the value of the variable after the function returns?
local

16. What do we call the default passing method which implies the creation of temporary variables?
Pass-by-valueYes. They must match exactly in order for the prototype to be accurately mapped to its definition.

17. What is a global variable? Where can it be accessed?
A variable defined outside of any function, it can be accessed from anywhere.

18. What happens to global variables when there is a local variable with the same name?
The local variable is used.

19. Why are global variables dangerous?
Harder to debug your program with so many variables in the same scope.

20. [Extra Credit] How short or long (how many lines) should a function be? (yes, there is a correct, but tricky answer)
Pretty sure there is no specific line size a function needs to be.

21. What can be passed as input to a function? What is the only requirement?
Any valid expression. That it matches your prototype paramter list.

22. How do you return a value from within a function?
return (whatever);

23. What is a “default value” for a parameter value? Where is it specified? What does it look like?
A default value is a value to use if none is supplied on user input. It is specified in the prototype. int myfunc(int x = 10);

24. Is this a valid prototype? What does it mean? “int MyIntFunction( int = 10, int = 20 );”
Yes, parameter names are not required. I have a function that has 2 default value integers, the funtion returns an integer.

It enables you to create more than one function with the same name so differant data types can be passed to the same function.

26. What is recursion? When is it useful?
When a function calls itself. It is useful when you have a problem which is best solved by performing the same calculation on the result of that calculation.

##### Share on other sites
Chapter 5.

1) Where do global functions exist? Where do member functions exist?
A) Global functions can exist outside of objects and classes, and member functions (sometimes called member methods) exist within a clas and do its work.

2) What is a function?
A) A function is, in effect, a subprogram that can act on data and return a value.

3) When a function identifier is encountered, where does the execution jump to? Where does it return after the function terminates?
A) It jumps to the body of the function, when it finishes, the function returns to the next line of the calling function.

4) How do you declare a function's return value type?
A) By writing the type that is going to be returned infront of the functions name.

5) What is a parameter list?
A) It is the description of the values you send to the function.

6) Can you use a function before it is declared? Can you use it before it has been defined?
A) Yes, you can use a function before it is declared, if you use the definition, you can't use a function before it is defined, because then it doesn't have a body yet.

7) If a function has been defined before being used, must I provide a separate function declaration (prototype)?
A) No, the definition will be used for both.

8) Is it wise to use definitions throughout you code in place of declarations? Why or why not?
A) No, it's not wise for the following reasons, first, it is a bad idea to require that functions appear in a file in a particular order. Doing so makes it hard to maintain the program when requirements change. Second, it is possible that function A() needs to be able to call functions B(), but function B() also needs to be able to call function A() under some circumstances. Third reason, function prototypes are a good and powerfull debugging technique. If your prototype declares that your function takes a particular set of parameters or that it returns a particular type of value, and then your function does not match the prototype, the compiler can flag your error instead of waiting for tit to show itself when you run the program. This is like double-entry bookkeeping. The prototype and the definition check each other, reducing the likelihood that a simple typo will lead to a bug in your program.

9) Is it necessary that a function prototype and definition agree EXACTLY about return type and signature?
A) Yes, they have to be exactly the same.

10) Must a function prototype include the names of variables in the parameter list?
A) No, it isn't obligated, but, it makes it much easier to determine the functions purpose.

11) Must the parameter names in the declaration (prototype) match the parameter names in the definition?
A) No, that is not necessary.

12) What do we call variables created within the scope of a block.
A) We call those local variables.

13) What happens to those variables when the function returns to a higher scope?
A) They go out of scope ( marked for destruction by the compiler).

14) Are these variables created on the stack, or the heap?
A) They are created on the stack.

15) By default, are parameter variables local or global? What does this mean for the value of the variable after the function returns?
A) They are local by default, it means that the variable has no inpact on any variable outside the scope of it's calling function.

16) What do we call the default passing method which implies the creation of temporary variables?
A) Pass by value.

17) What is a global variable? Where can it be accessed?
A) A global variable is defined outside of any function, and thus available from any function in the program, including main().

18) What happens to global variables when there is a local variable with the same name?
A) It will hide the global variable.

19) Why are global variables dangerous?
A) Because they are shared data, and one function can change a global variable in a way that is invisible to another function. This can and does create bugs that are very difficult to find.

20) How short or long (how many lines) should a function be? (yes, there is a correct, but tricky answer).
A) I think 1, because you can use a definition as declaration and put the body on the same line if it is not to big.

21) What can be passed as input to a function? What is the only requirement?
A) I have no idea !!!!!!

22) How do you return a value within a function?
A) By using the return keyword

23) What is a "default value" for a parameter value? Where is it specified? What does it look like?
A) A "default value" is a value to use if none is supplied. It is specified in the prototype of the function like this: void myFunc(int length, int width = 1, int height = 2);

24) Is this a valid prototype? What does it mean? "int MyIntFunction(int = 10, int = 20);".
A) Yes, it is valid, it means that the first parameter of type int will get a default value of 10 and the second a default value of 20. Remember, a functions parameters is identified by position, not name.

A) C++ enables you to create more than one function with the same name, the benefit is that you can use one function name to calculate the square of f.e. an int, float, double without having to change the function name.

26) What is recursion? When is it useful?
A) Recursion is a function that calls itself directly or indirectly. It's useful in situations where you act on data and then act in the same way on the result.

##### Share on other sites
Chapter 5 Quiz

1. Where do global functions exist? Where do member functions exist?
Global functions are in the main code, member functions are in classes

2. What is a function?
A set of instructions executed when the functions name is called.

The execution jumps to the function and executes it then back to the line IMMEDIATELY FOLLOWING the line which the function was called.

4. How do you declare a function’s return value type?
At the beginning of the function's header, example: int myFunc() is a function thta will return an integer.

5. What is a parameter list?
The list of parameters the function will use, these parameters are variables which are passed to the function.

6. Can you use a function before it has been declared? Can you use it before it has been defined?
The prototype must be declared at the beginning of the code. You can use the function before it's defined however

7. If a function has been defined before being used, must I provide a separate function declaration (prototype)?
It is not necessary, However the function definition must be placed before the first usage of the function.

8. Is it wise to use definitions throughout your code in place of declarations? Why or why not?
No, it the definitions call on another function which calls on the current function, there would be some problems.

9. Is it necessary that a function prototype and definition agree EXACTLY about return type and signature?
Yes

10. Must a function prototype includes the names of variables in the parameter list?
No, just the types.

11. Must the parameter names in the declaration (prototype) match the parameter names in the definition?
Yes

12. What do we call variables created within the scope of a block?
Local variables

13. What happens to those variables when the function returns to a higher scope?
They are marked for destruction by the compiler

14. [Extra Credit] Are these variables created on the stack, or the heap?
The stack

15. By default, are parameter variables local or global? What does this mean for the value of the variable after the function returns?
local

16. What do we call the default passing method which implies the creation of temporary variables?
passing by values

17. What is a global variable? Where can it be accessed?
A global variable is a variable declared outside a black, and can be accessed by any functions

18. What happens to global variables when there is a local variable with the same name?
The black with the local variable will ignore the global and use the local variable but just within that block.

19. Why are global variables dangerous?
Functions can change them and stuff, so they may get messed up.

20. [Extra Credit] How short or long (how many lines) should a function be? (yes, there is a correct, but tricky answer)
Depending on what it is used for. Maybe 3 - 50 lines.

21. What can be passed as input to a function? What is the only requirement?
Anything that is the same type as the parameter specifies, a statement or expression could be used

22. How do you return a value from within a function?
using the return command

23. What is a “default value” for a parameter value? Where is it specified? What does it look like?
The value that will be used if no value is given, in int myFunc(int x = 14) 14 is the default value for the parameter x.

24. Is this a valid prototype? What does it mean? “int MyIntFunction( int = 10, int = 20 );”
Yes

Making more than one function with the same name, however they are differentiated by how many parameters, or different types.

26. What is recursion? When is it useful?
A function will call itself, leading to a loop.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 12
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632152
• Total Posts
3004466

×