In C, must every function return a value, or are non-returning (void) functions valid? Answer for standard C across common platforms.

Difficulty: Easy

Correct Answer: No

Explanation:


Introduction / Context:
Functions in C can return values, but many procedures exist solely for their side effects. Understanding return types, especially void, is fundamental to API design and reading system headers (for example, qsort callbacks, signal handlers with defined signatures).


Given Data / Assumptions:

  • ISO C rules for function return types apply.
  • We consider typical hosted implementations (not freestanding edge cases).


Concept / Approach:
A function declared with void return type does not return a value (for example, void log_msg(const char*)). Returning a value from a void function is ill-formed. Conversely, a non-void function must return a value along every control path to avoid undefined behavior. Therefore, “every function must return a value” is false in general because void functions are perfectly valid and common.


Step-by-Step Solution:

Identify claim: all functions must return a value.Recall: void functions exist in standard C.Therefore: the claim is false; only non-void functions must return a value.


Verification / Alternative check:
Review standard headers such as stdlib.h (e.g., void qsort). You will find many void-return functions in the standard library and typical codebases.


Why Other Options Are Wrong:

Yes: Incorrect; void functions contradict this.Only in C++ are void functions valid: Incorrect; C supports void extensively.Only main can be void: In hosted C, main must return int; declaring main as void is non-standard.Only on embedded systems can you omit returns: Not a language rule; void functions are standard everywhere.


Common Pitfalls:
Confusing a function that returns no value (void) with falling off the end of a non-void function, which is undefined behavior.


Final Answer:
No.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion