Topic 9 - Functions Flashcards
What do functions consist of:
1) Declarations
2) Statements
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?
C89 = don't need it; default returns value of type int C99 = omitting return type is illegal
In C89, where must you place variable declarations ?
In C99?
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
Can variables declared in the body of a function be examined or modified by other functions?
No.
How do you make it clear that you are deliberately discarding the return value of a function?
Put (void) in front of the function call.
Does C89 care where definition of a function is placed relative to where it is being called?
How about C99?
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.
Does it matter where we place the function’s definition?
Yes.
What happens if a function’s definition is placed after where it is being called?
- 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
What is an implicit declaration?
When function’s defn is after its call and the compiler assumes its returning an int type
What is default argument promotions?
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
Give 2 ways to remedy the problem of call-before-definition:
1) function den precedes call
2) use function declarations
What is a function declaration’s syntax?
return-type function-name (parameters); ex: double average(double a, double b); /* DECLARATION */
Can you omit the parameter names? (As long as you still specify the parameter types)?
Yes, this is called function prototyping
Does any changes made to the parameter during the execution of the function affect the arguments?
No.
Does argument decomposition work?
No.
In terms of Argument Conversions: if the compiler has encountered a prototype prior to the call - what happens?
The value of each argument is implicitly converted to the type of the corresponding parameter as if by assignment
In terms of Argument Conversions: if the compiler has NOT encountered a prototype prior to the call - what happens?
- compiler performs default argument promotions
- float arguments to be converted to double
- char and short arguments convert to int
Is there an easy way for a function to know the length of an array that has been passed as an argument?
No, usually have to pass a second argument being the length of the array.
Do you put the brackets in an array argument in the arguments section of the function header?
No:
total = sum_array(b[], LEN);
is wrong. This is right:
total = sum_array(b, LEN);
Can a function change the elements in an array parameter where the chance is reflected in the corresponding argument?
Yes.
If a parameter is a multidimensional array, which dimension can the length be left unspecified?
the length of the first dimension.
How do you remedy the annoyance of not being able to pass multidimensional arrays with an arbitrary number of columns?
Use arrays of pointers (chapter 13)
Do you need a return statement in a void function?
No but you can have it if you wish but unnecessary.
Give 2 ways to terminate a program:
1) return statement in main
2) exit function anywhere in the program if you’ve included
How do you improve quick sort program’s performance?
- Improve the partitioning algorithm
- use a different method to sort small arrays
- make quick sort non recursive