Consider the function declaration: void func(int x); and later in the code: int result = func(5);. What will be the compilation result?
ACompilation error because func returns void but is assigned to an int variable
BCompilation warning but will compile successfully; result will be undefined
CSuccessful compilation and result will be 5
DRuntime error will occur
Correct Answer:
A. Compilation error because func returns void but is assigned to an int variable
EXPLANATION
A void function cannot return a value. Attempting to assign the return value of a void function to a variable will cause a compilation error in standard C compilers.
What is the difference between declaring a function and defining a function?
ADeclaration specifies the function signature; definition includes the function body
BThey are the same thing in C
CDefinition comes before declaration
DDeclaration includes the function body; definition does not
Correct Answer:
A. Declaration specifies the function signature; definition includes the function body
EXPLANATION
A declaration tells the compiler about the function's signature (return type, name, parameters); a definition provides the actual implementation (function body).
In C, what is the purpose of the return statement in a void function?
AIt is optional and not needed
BTo return 0 by default
CTo exit the function immediately without returning a value
DTo cause compilation error if used
Correct Answer:
C. To exit the function immediately without returning a value
EXPLANATION
In void functions, return statement (without value) can be used to exit the function prematurely. It's optional at the end since function exits automatically.
What happens when a function is called without a return statement reaching the end?
ACompilation error occurs
BAn undefined/garbage value is returned
C0 is automatically returned
DNULL is returned
Correct Answer:
B. An undefined/garbage value is returned
EXPLANATION
If a non-void function doesn't explicitly return a value, it returns whatever garbage value happens to be in the return register, leading to undefined behavior.
Which of the following is a valid function declaration in C?
Aint func(void);
Bfunc int();
Cvoid int func();
Dint func() void;
Correct Answer:
A. int func(void);
EXPLANATION
The correct syntax is return_type function_name(parameters). Option A follows proper C syntax with int as return type, func as name, and void indicating no parameters.