COSC-1337 Chapter 6 Flashcards

1
Q

What is the approach of using functions in a program known as?

A

Divide and conquer.

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

What is the benefit of using functions in your program?

A

Code reuse. You can reuse the same functions throughout the entire program without having to rewrite them over and over.

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

What is a “function call”?

A

It is a statement that causes a function to execute.

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

What is the first line of a function called?

A

Function header

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

Just like header files in our program, what do we need for function to be included in our program as well?

A

Function prototypes. These come before the main function begins.

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

Values that are sent to a function are called?:

A

Arguments

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

The “return” statement will cause a function to do what?

A

End immediately.

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

A function that returns a value is known as a?

A

Value-returning function

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

Variables that are defined inside of a function are known as?

A

Local variables

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

A local variable can only be accessed from?

A

Within the function that created it

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

A global variable can be accessed from?

A

Anywhere in the program

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

What happens to local variables after their function’s end?

A

The “lifetime” of the local variable is destroyed.

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

Where do you have to declare a variable for it to be “global”?

A

Outside of all functions INCLUDING MAIN

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

How would you clear the screen?

A

system(“clear”);

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

What happens if the name of a Global Variable/Constant is the same as a local variable in a function?

A

The global variable is “shadowed” by the local variable in the function.

This means that the global variable is not seen by the local variable in the function with the same name.

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

How would we create a local variable in a function that would remember its value after ever time the function is called?

A

function(int x, int y) {

static int localVar;

localVar = x+y;

return localVar;
}

17
Q

What happens when there is no argument that is passed to a functions parameter when the function is called?

A

The “default arguments” are input to the function.

18
Q

How can we initialize the default argument values of a function?

A

By declaring them in the “function prototype”

19
Q

How do we change the value of a variable that was passed to a function as an argument?

A

We would use a reference variable. This is known as “pass by reference”.

function(int &staticVar, int y) {

statements;
statements;
return value;
}

In the example above, the parameter “staticVar” will also change the value of the variable that is passed to it in the function call.

20
Q

Are variables the only thing that can be “passed by reference”?

A

Yes. Passing anything else by reference will result in an error.