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.