03 - Functions Flashcards

1
Q

Functions in C are most like what in Java?

  • Static methods
  • Public methods
  • Private methods
A

Static methods

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

How many options can an ‘‘if’ statement choose between?

A

2
More conditions can be used by breaking them down into two compound conditions.

e.g. if ((x || y) && z){}

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

What will be the final value of ‘x’ after the following code?

int x = -1;
if(x){
    x = 1
} else {
    x = 2
}
A

x = 1

An if statement will count any non-zero value as true

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

What is a function prototype?

A

A function prototype is a way of declaring a function without its body. It simply lets the program know that the function exists, allowing the compiler to check its use, without knowing what it actually calculates/outputs

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

Is this a valid function prototype?

int add(int, int);

A

Yes

It’s not necessary to give names to the arguments - just their types.

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

Which of these are you not likely to find in a header file?

  • Function prototypes
  • Macro definitions
  • Function definitions
  • Constant variable declarations
A

Function definitions

These cannot be included in a header file that is intended to be used in more than one C file, as a function can only be defined once in the entire program

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

What happens if you use a function without declaring it first?

A

C will guess a prototype, based on how it has been called.

It will assume the function returns an integer, and will guess the argument types based on what you have provided

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