Function prototypes tell the computer about a function before it is used. This helps avoid mistakes and makes the program easier to understand.
Function prototypes
return_type function_name(parameter_type1, parameter_type2, ...);
The prototype ends with a semicolon.
It shows the function name, return type, and parameter types but not the function body.
add that takes two integers and returns an integer.int add(int, int);
printMessage that takes no parameters and returns nothing.void printMessage(void);
average that takes three floats and returns a float.float average(float, float, float);
This program declares the function multiply before main using a prototype. Then it defines the function after main. The program multiplies 4 and 5 and prints the result.
#include <stdio.h> // Function prototype int multiply(int a, int b); int main() { int result = multiply(4, 5); printf("4 times 5 is %d\n", result); return 0; } // Function definition int multiply(int a, int b) { return a * b; }
Function prototypes help the compiler check if you call functions with the right number and type of arguments.
If you forget a prototype, the compiler may assume the function returns an int, which can cause errors.
Using prototypes makes your code easier to read and maintain.
Function prototypes tell the compiler about functions before they are used.
They include the function name, return type, and parameter types but no body.
Prototypes help avoid errors and improve code organization.