Docsity

C++ Exam 6: Modular Programming, Functions, and Reference Variables, Exams of Advanced Education

Toggle sidebar
bg1
C++ EXAM 6 QUESTION AND ANSWER
modular programming - breaking up program into smaller parts
Function parts - return type- data type of value that called it
name -follows same rule as varibles
parameter list- variables containing values passed to function
body-enclosed in {}
function definition - a collection of statements to create a task
2 benefits of modular programming - improves maintaniblity
simplifies writing process
void - If a function doesn't return a value, its return type is void
If a function returns a value, what must it include? - the type of value must be indicated, such as
int main. Main can not be void
How do you call a function? - name, () and ; such as printHeading ();
How many functions can main call? - Any number of functions
What must the compiler know about the function before it is called? - name, return type,
number of parameters, data type of parameter.
function prototypes - ways to notify the compiler about a function before a call to the function
pf3
pf4
pf5
pf8

Partial preview of the text

Download C++ Exam 6: Modular Programming, Functions, and Reference Variables and more Exams Advanced Education in PDF only on Docsity!

C++ EXAM 6 QUESTION AND ANSWER

modular programming - breaking up program into smaller parts Function parts - return type- data type of value that called it name -follows same rule as varibles parameter list- variables containing values passed to function body-enclosed in {} function definition - a collection of statements to create a task 2 benefits of modular programming - improves maintaniblity simplifies writing process void - If a function doesn't return a value, its return type is void If a function returns a value, what must it include? - the type of value must be indicated, such as int main. Main can not be void How do you call a function? - name, () and ; such as printHeading (); How many functions can main call? - Any number of functions What must the compiler know about the function before it is called? - name, return type, number of parameters, data type of parameter. function prototypes - ways to notify the compiler about a function before a call to the function

Difference between header and prototype - close with semicolon for prototype. ex void printHeading () header void printHeading (); for prototype Prototypes need to have the types declared, headings do not. Say int fun 1(int a, double x). What is the calling for the header and for the prototype - heading fun1(b,y); prototype fun1(int, double); polymorphism - functions can be called the same, making it easier for programmer for flexiblity. The type has to be the same. Where do prototypes go? - Near the top of the program after libraries and before int main (), but don't have to worry about the order. Where are functions with a power in? - What are values passed to a function called? - arguements such as in pow(a, b) the a, b is the arguments Parameters - variables in a function that hold the values passed as arguements like the pow Why should you use instead of making your own math function? - less errors, better to read, efficient, less code to be maintain, programming standards another name for parameter and arguments - formal or actual

What can you do to return more then one value? - write a function and then use it to return value. Can use a global variable but not recommended. Globals should be constants. Can you use a return in a void function? Will you be able to use it to return a value? - Yes, but it won't return a value Where will a value returning funtion return a value to? - The point of the call What is the difference between coercion vs casting? - coercion is automatic and casting is ordered by the programmer. What must be indicated when returning the value from a function? - The prototype adn the definition must indicate the data type except void. example of returning value for assigning it to a vlaue - a = pow(x,y); example of sending returning value to cout - cout << pow(x,y); example of sending returning value in an expression - area = PI * square (x); What is the difference between system programming verses application programming. - system programming is to interact with the computer, like for the system. application programming is program for users Boolean value - only part of C++, returns true or false. Declaring boolean values - bool such as bool myValue; can use it as a type for the function

Why are functions called first class functions in C++? - Can do functions inside of functions. recursion - function calling itself. iteration - Stops at a certain number and done Difference between global and local functions - global outside of function, local inside. Lifetime is the same as the function. Scope, is after where it is declared in global. Advantage of local variables - Hidden from the statements in functions. Can use the same name because the scope is different. Why can local and global names be the same but have different results? - They are in different memory spaces. What happens to the values of local variables after the lifetime of the function? - It is lost between calls to the function in which it was declared. They are destroyed in the memory space. Which is better to use, global variables or global constants? Why? - You should use global constants. Global variables make it harder to debug. Advantage of global constants. - If you need to change everything, you just have to change the global constant. Only have to change once. Intializing local variables - local variables are not automatically initialized, except in newer types. Must be initialized by the programer. like int a; cout << a;

What happens to changes with a reference variable? - They are made to the variables it is referenced to. Do you have to use & in both the prototype and header for a reference variable? Can the argument be an expression or constant? - Yes No When should you use a reference variable? When should you not use it? - You should use it when it is appropriate. Do not use it when the arguement should not be changed by a function or when it's function needs to return only 1 value. Overloading functions - The functions have the same name but different parameter lists. What are overloading functions used for? - They are used to create functions that perform the same tasks but take different parameter types or different number of parameters. What does the exit() function do? - Terminates the execution of a program. What functions can call an exit() function? - Any Can exit() functions pass a value? - yes What header does the exit() function require? - cstdlib header file. What are stubs and drivers useful for? - testing and debugging program and function logic and design.

What is a stub? - A dummy function used inh place of an actual function. It usually displays a message indicating it was called. May also display parameters. What is a driver? - A function that tests another function by calling it. Various arguments are passed and return values are tested