Topic 9 - Functions Flashcards

1
Q

What do functions consist of:

A

1) Declarations

2) Statements

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

Do you need a return statement in C89? If not, what happens when no return statement?
In C99? If not, what happens when no return statement?

A
C89 = don't need it; default returns value of type int
C99 = omitting return type is illegal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In C89, where must you place variable declarations ?

In C99?

A
C89 = Must come first before all statements in the body of a function.
C99 = variable declarations and statements can be mixed, as long as each variable is declared prior to the first statement that uses the variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can variables declared in the body of a function be examined or modified by other functions?

A

No.

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

How do you make it clear that you are deliberately discarding the return value of a function?

A

Put (void) in front of the function call.

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

Does C89 care where definition of a function is placed relative to where it is being called?
How about C99?

A

No - C89 does not require that the definition of a function precedes its calls
C99 will give you an error if calling a function without first providing a definition or declaration of the function.

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

Does it matter where we place the function’s definition?

A

Yes.

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

What happens if a function’s definition is placed after where it is being called?

A
  • compiler encounters function call but has no information on it
  • doesn’t produce an error message but assumes the function returns an int and hopes for the best
  • this is called an IMPLICIT DECLARATION
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an implicit declaration?

A

When function’s defn is after its call and the compiler assumes its returning an int type

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

What is default argument promotions?

A

The compiler is unable to check that we are passing to the function the right number of arguments and that the arguments have the proper type:

  • float arguments to be converted to double
  • char and short arguments convert to int
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Give 2 ways to remedy the problem of call-before-definition:

A

1) function den precedes call

2) use function declarations

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

What is a function declaration’s syntax?

A
return-type function-name (parameters);
ex:
double average(double a, double b);   /* DECLARATION */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you omit the parameter names? (As long as you still specify the parameter types)?

A

Yes, this is called function prototyping

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

Does any changes made to the parameter during the execution of the function affect the arguments?

A

No.

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

Does argument decomposition work?

A

No.

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

In terms of Argument Conversions: if the compiler has encountered a prototype prior to the call - what happens?

A

The value of each argument is implicitly converted to the type of the corresponding parameter as if by assignment

17
Q

In terms of Argument Conversions: if the compiler has NOT encountered a prototype prior to the call - what happens?

A
  • compiler performs default argument promotions
  • float arguments to be converted to double
  • char and short arguments convert to int
18
Q

Is there an easy way for a function to know the length of an array that has been passed as an argument?

A

No, usually have to pass a second argument being the length of the array.

19
Q

Do you put the brackets in an array argument in the arguments section of the function header?

A

No:
total = sum_array(b[], LEN);
is wrong. This is right:
total = sum_array(b, LEN);

20
Q

Can a function change the elements in an array parameter where the chance is reflected in the corresponding argument?

A

Yes.

21
Q

If a parameter is a multidimensional array, which dimension can the length be left unspecified?

A

the length of the first dimension.

22
Q

How do you remedy the annoyance of not being able to pass multidimensional arrays with an arbitrary number of columns?

A

Use arrays of pointers (chapter 13)

23
Q

Do you need a return statement in a void function?

A

No but you can have it if you wish but unnecessary.

24
Q

Give 2 ways to terminate a program:

A

1) return statement in main

2) exit function anywhere in the program if you’ve included

25
Q

How do you improve quick sort program’s performance?

A
  • Improve the partitioning algorithm
  • use a different method to sort small arrays
  • make quick sort non recursive