A function prototype declares a function’s name, return type, and parameter types before the function is called. It allows the compiler to check function calls against the declared interface.
#include <stdio.h>
int multiply(int a, int b);
int main(void)
{
int result = multiply(4, 5);
printf("Result = %d\n", result);
return 0;
}
int multiply(int a, int b)
{
return a * b;
}
Example Output #
Result = 20
Explanation #
The declaration:
int multiply(int a, int b);
is the function prototype. It tells the compiler that multiply returns an int and accepts two int parameters.
The prototype ends with a semicolon because it declares the function without defining its body.
The function is called before its definition appears:
int result = multiply(4, 5);
The prototype makes the function’s interface known at this point.
The function definition appears later:
int multiply(int a, int b)
{
return a * b;
}
Function Prototype Syntax #
return_type function_name(parameter_types);
| Prototype | Meaning |
|---|---|
int add(int, int); |
Returns int; accepts two int arguments |
float calculate(float, float); |
Returns float; accepts two float arguments |
void display(void); |
Returns no value and accepts no parameters |
char getLetter(void); |
Returns char and accepts no parameters |
Parameter names may be included or omitted in a prototype:
int add(int, int);
int add(int first, int second);
Both declarations specify the same function interface.
In a function prototype, (void) explicitly means that the function accepts no parameters. An empty parameter list () has a different meaning in a declaration in C: it does not specify the parameter types. For a fully specified function interface, use (void) when no parameters are accepted.