Functions and Void Return Types
C Functions and Prototypes
A function lets you name a task, supply inputs, and optionally receive a result. When reading C functions and prototypes, separate three questions: what interface is declared, what work the definition performs, and how a caller uses the result. The examples here use C17 and explicit parameter lists. You will encounter these patterns when splitting a systems program into small helpers for calculations, logging, and other tasks.
Read the declared interface
A function prototype is a declaration that specifies a function's return type and parameter types. A separate prototype ends with a semicolon:
int stored_bytes(int count, int bytes_each);The return type specifies the type of value a function returns, or uses void to indicate that no value is returned. Here, the result has type int. The parameters also have type int, in the order count, then bytes_each.
Parameter names help readers, but they are optional in a separate prototype. The following declaration describes the same interface:
int stored_bytes(int, int);Declarations for the same function must have compatible types. Changing parameter names does not change the interface; changing the return type or swapping distinct parameter types can make declarations incompatible. A prototype describes the interface without executing the function or supplying its body.
Connect a definition to a call
The definition supplies the executable body:
int stored_bytes(int count, int bytes_each)
{
return count * bytes_each;
}For this helper, the governing calculation is
where n is the number of items, b is the number of bytes per item, and B is the total number of bytes.
Example: A caller executes int total = stored_bytes(4, 6);. The arguments supply the values 4 and 6. The function calculates 4×6=24 bytes, and return makes 24 the value of the call expression. The initialization stores that value in total.
C passes these scalar inputs by value: the parameters receive copies. Assigning a new value to count inside the helper would not assign to the caller's variable.
A visible prototype also determines argument conversions. An arithmetic argument need not already have exactly the parameter's type. For example, passing 1.8 to an int parameter supplies 1, because conversion to int discards the fractional part toward zero when the result is representable.
Make the prototype visible before the call. A compatible definition placed before the call already provides this information; otherwise, place a separate prototype earlier.
Interpret void by its position
The two uses of void answer independent questions.
Prototype | Inputs | Returned value |
|---|---|---|
int stored_bytes(int count, int bytes_each); | Two integers | An integer |
void log_bytes(int bytes); | One integer | No value |
int sample_count(void); | No arguments | An integer |
void reset_logger(void); | No arguments | No value |
A void function can still perform work, such as recording a message. Call it as a statement, such as log_bytes(total);. No returned value is available for assignment or arithmetic; void does not mean a returned zero.
Inside a void function, return; exits immediately. Reaching the closing brace also returns control to the caller. In C17, return expression; is not permitted in a void function, even when that expression itself has type void.
For an ordinary value-returning helper, provide a suitable return expression on every path whose result the caller will use. A missing return does not promise zero. Conversely, a caller may discard a legitimate returned value by using the call as a statement.
What to remember
- A prototype specifies the return type and ordered parameter types.
- Parameter names may differ between a prototype and its definition.
- Scalar parameters receive copies of argument values.
- Leading void means no returned value; (void) means no arguments.
- Check argument conversions before tracing a returned result.
Example: Logging a telemetry packet
A telemetry program represents packet sizes as integer numbers of bytes. It uses a function prototype, a declaration specifying return and parameter types, to make a helper's interface available before its definition. The following C17 helpers build and record a packet size:
void log_bytes(int bytes);
int sample_bytes(int count, int width)
{
return count * width;
}
int add_header(int payload)
{
return payload + 3;
}
void record_packet(int count, int width)
{
int total = add_header(sample_bytes(count, width));
log_bytes(total);
return;
}The supplied implementation of log_bytes records its integer argument and returns no value. Each sample has a fixed width, and every packet has a three-byte header.
a) Another function must call record_packet before its definition appears. Write a compatible prototype using the parameter names count and width.
Read the definition's interface. The return type is void, and its two ordered parameters both have type int. Copy that interface and finish the declaration with a semicolon:
void record_packet(int count, int width);This declaration allows earlier calls to be checked against the interface. The leading void specifies no returned value; it does not remove the two integer parameters.
b) A caller executes record_packet(9, 12); for nine samples, each occupying twelve bytes. What byte count is passed to log_bytes?
First, the wrapper receives a sample count of ________ and a width of ________. It supplies these values to sample_bytes.
Calculate the payload size:
The inner call therefore returns ________. That result becomes the payload argument of add_header, which adds the three-byte header:
The wrapper initializes its local total to 111 and passes that integer to the logging function. The final return; then returns control without supplying a value.
Answer: log_bytes receives 111 bytes.
c) A programmer writes int saved = record_packet(9, 12);. Replace this statement so it records the packet size without attempting to save a returned value.
Check the wrapper's declared return type before using the call in an expression. record_packet returns void, so its call does not provide an integer that can initialize saved. The local variable total inside the wrapper does not automatically become a returned value.
Keep the call as a statement:
record_packet(9, 12);This still runs the helper calculations and records 111 bytes. It simply avoids an invalid initialization from a call that returns no value.
A C17 packet builder uses this helper to add a six-byte header to a payload length:
int add_header(int bytes)
{
bytes = bytes + 6;
return bytes;
}A caller then executes:
int length = 38;
int packet = add_header(length);After both statements finish, give the values of length and packet in bytes. Enter each as an exact integer.
A C17 measurement program already contains this file-scope declaration:
double elapsed_ms(int samples, double interval);Evaluate each proposed file-scope redeclaration independently. Enter compatible or incompatible according to whether it agrees with the existing function type. Do not treat the rows as declarations added together.
| Proposed declaration | Verdict |
| double elapsed_ms(int count, double ms_each); | |
| int elapsed_ms(int count, double ms_each); | |
| double elapsed_ms(double samples, int interval); | |
| double elapsed_ms(int, double); |