Chapter 6 - Functions Flashcards

1
Q

Define: Code reuse

A

Write the code to perform a task ONCE, then reuse it each time you need to perform the task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define: Function call

A

A statement that causes a function to execute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define: Function Definition

A

Contains the statements that make up the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

All function definitions have what 4 parts?

A

1) Return Type
2) Name
3) Parameter list
4) Body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define: Return type (of a function)

A
#1
data type of the value that is sent FROM the function 
listed first 
int main() --> int is the RETURN TYPE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Define: Name (of a function)

A
#2
Should give each function a descriptive name - in general, same rules apply as to variables for naming
int main() --> main is the NAME
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define: Parameter List (of a function)

A
#3
List of variables that hold the values being passed TO the function
int main() --> () is the parameter list (it's empty!)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define: Body (of a function)

A
The set of statements that perform the function's operation. 
enclosed in a set of braces
int main() 
{
BODY HERE
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Define: Function Header

A

The line in the definition that reads int main() (the first line)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define: Void functions (they return….)

A
Some function simply perform one or more statements and then terminate, without returning a value. these are VOID functions. 
Example:
void displayMessage()
{
     cout << "hello from the function displayMessage. \n";
}

Notice: not return statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
void displayMessage()
{
     cout << "hello from the function displayMessage. \n";
}
What part is the function header? 
How would you "call" this function?
A

Function Header: void displayMessage()

Call: displayMessage();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Fuction prototypes are also known as …

A

Function declarations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

You must place either the function definition or the function prototype _______ all calls to the functions.
What happens if you don’t?

A

BEFORE

Or else the program will not compile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What would a “function prototype” for the following function definition be? What’s different about it?

void displayMessage()
{
     cout << "hello from the function displayMessage. \n";
}
A
void displayMessage();
- semicolon at the end

Tells compiler the function displayMessage has a void return type (doesn’t return a value) and uses no parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

VALUES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Values sent into a function are called ___________.

A

arguments

17
Q

Define: Parameter

A

a special variable that holds a value being passed into a function.
For example:
void displayValue(int num)
{
court &laquo_space;“The value is “ &laquo_space;num &laquo_space;endl;
}
The variable num is the parameter.

18
Q
For this function definition, what would the prototype be that you could use at the top?
void displayValue(int num)
{
     court << "The value is " << num << endl;
}
A

void displayValue(int);

19
Q
How would you call the function of this prototype:
void displayValue(int);
A

displayValue(5); (this could be any integer where the argument 5 is)

20
Q

What could the function body be for the following function:

void displayValue(int);

A
void displayValue(int num)
{
     cout << "The value is " << num << endl;
}
21
Q

WARNING!
When passing a variable as an argument, simply write the variable name inside the parenthese of the function call. Do not write the _____ _____ of the argument.

A

Data Type.
displayValue(int x); // Error!

Call should appear as:
displayValue(x); // Correct

22
Q

If you pass an argument whose TYPE is not the same as the parameter’s type, the argument will be ________ or _________ automatically.

A

promoted or demoted
Ex: the following function call would be truncated (because the parameter is an integer not double):
displayValue(4.7);

23
Q

WARNING!
Each parameter variable in a parameter list must have a _____ _____ listed before its name.
Provide an example.

A
Data Type:
void showSum (int num1, num2,  num3) // ERROR!

void showSum (int num1, int num2, int num3) // Correct

24
Q

When a function with multiple parameters is called, the arguments are passed to the parameter in ______.

A

order.

example: showSum(5, 10, 15); passes 5, 10, 15 in that order ….

25
Q

When an argument is passed into a parameter, only a _____ of the argument’s value is passed.

Changes to the parameter do not affect the ______ argument.

A

When an argument is passed into a parameter, only a copy of the argument’s value is passed.

Changes to the parameter do not affect the original argument.

26
Q

The return Statement causes a function to …

A

end immediately

27
Q

Functions that return a value are known as….

A

value-returning functions

28
Q

The power function is an example of a ______-_____ function.
double x;
x = pow(4.0, 2.0);

A

value-returning function
double x;
x = pow(4.0, 2.0);

The 2nd line calls the pow function, passing 4.0 and 2.0 as arguments.
the function calculates the value of 4.0 raised to the power of 2.0 and returns the value, which is 16.0 and is assigned to the x variable by the = operator.

29
Q

Although several arguments may be passed into a function, only one ______ may be returned from it.

A

value

30
Q
int sum(int num1, int num2)
What is the Return Type?
A

int

31
Q

What is the general format of a return statement?

A

return expression;

32
Q

The return expression can be any expression that has a _______, such as…..

A

The return expression can be any expression that has a value, such as a variable, literal, or mathematical expression.

33
Q

A local variable is defined ______ a function and not accessible ______ the function.

A

A local variable is defined inside a function and not accessible outside the function.