Open In App

Function Prototype in C

Last Updated : 10 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In C, a function prototype is a statement that tells the compiler about the function’s name, its return type, numbers, and data types of its parameters. Function prototype provides a means for the compiler to cross-check function parameters and their data type with the function definition and the function call.

Function-Prototype-in-c

For example:

C
// Not a prototype as it does not contain the number
// and type of parameters. It is only declaration
int sum();

// Valid prototype as it contains function name,
// return type, number and type of parameters
int sum(int, int);

// Also a valid prototype
int sum(int a, int b);

// Function definition inherently contains function
// prototype
int sum(int a, int b) {
  	return a + b;
}

The above program contains a function prototype for a function named sum that adds two numbers which it takes as arguments and returns their sum.

Syntax For Function Prototype in C

C
return_type name(type1, type2 ....);

where,

  • return_type : Type of value the function returns.
  • name: Name of the function
  • type1, type2 ....: Type of the parameters. Specifying their names is optional.

Function prototype can be used in place of function declaration in cases where the function reference or call is present before the function definition but if the function definition is present before the function call in the program then the function prototype is not necessary.

Example:

The following program demonstrates the usage of function prototype.

C
#include <stdio.h>

int sum(int x, int y); 
int main() {
	int x = 5, y=10; 
	
	printf("Sum of x and y is %d\n",sum(x, y));

	return 0;
}

int sum (int x , int y)
{
    return x+y; 
}

In the above program the function sum is called before its definition, since the program contains prototype for the sum function the program executes without any errors.

What Happens If the Function Prototype Is Missing?

When the function prototype is missing in C, the compiler generally gives two warnings:

  1. Implicit Declaration Warning.
  2. Incorrect return type warning (in case where the function should return non-integer value)

The first warning shows that the compiler cannot find the function with the given name. The other warning is due to the fact that the compiler assumes the return type to be int by default in case of missing return type(or whole prototype).

The below program demonstrate this:

C
#include <stdio.h>

int main() {
	int x = 5, y=10; 
	
	printf("Sum of x and y is %d\n",sum(x, y));

	return 0;
}

int sum (int x , int y)
{
    return x+y; 
}


Output

ERROR!
/tmp/EzfZGjMXJ4/main.c: In function 'main':
/tmp/EzfZGjMXJ4/main.c:6:41: error: implicit declaration of function 'sum' [-Wimplicit-function-declaration]
6 | printf("Sum of x and y is %d\n",sum(x, y));
| ^~~

Explanation
In the above program the function sum is called before it is defined which results in an implicit declaration error.

Benifits of Function Prototypes

Following are the major benefits of including function prototypes in your program:

  • Function Declaration Before Definition: It allows a function to be called before function definition. In large programs, it is common to place function prototypes at the beginning of the code or in header files, enabling function calls to occur before the function’s actual implementation.
  • Type Checking: A function prototype allows the compiler to check that the correct number and type of arguments are passed to the function. If the function is called with the wrong type or number of parameters, the compiler can catch the error.
  • Code Clarity: By declaring prototypes, you inform the programmer about the function's purpose and expected parameters, which helps in understanding the code structure.

Function Declaration vs Prototype

The terms function declaration and function prototypes are often used interchangeably but they are different in the purpose and their meaning. Following are the major differences between the function declaration and function prototype in C:

Function Declaration

Function Prototype

Function Declaration is used to tell the existence of a function.The function prototype tells the compiler about the existence and signature of the function.
A function declaration is valid even with only function name and return type.                                                                                                      A function prototype is a function delcaration that provides the function’s name, return type, and parameter list without including the function body.
Typically used in header files to declare functions.Used to declare functions before their actual definitions.

Syntax:

return_type function_name();

Syntax:

return_type function_name(parameter_list);


Next Article
Article Tags :

Similar Reads