Function Prototype in C



A function prototype in C programming is a declaration that specifies the function's name, its return type, and the number and data types of its parameters. A function in C is a block of code that performs a specific task.

What is a Function Prototype?

A function prototype allows the compiler to verify that the arguments and the data types of a function match with the ones that are specified in its declaration and its call.

The following image illustrates the syntax and structure of a function prototype −

Function Prototype

Ways to Write Function Prototypes in C

There are several different ways to write a function prototype in C. These prototypes are commonly used in C programming to declare functions before their actual definition.

// Example 1: Function declaration without parameters 
// (does not specify the number and type of parameters)
int function_name();

// Example 2: Function prototype with data types only 
// (specifies return type, number, and types of parameters)
int function_name(int, int);

// Example 3: Function prototype with parameter names 
// (valid and commonly used)
int function_name(int a, int b);

// Example 4: Function definition (which itself serves as a prototype)
int sum(int a, int b) {
    return a + b;
}

Example: Sum of Two Numbers

In this example, we calculate the sum of two numbers using a function prototype −

#include <stdio.h>

// function prototype
int sum(int a, int b); 

int main() {
   int a = 10, b = 20; 
   printf("Sum of a and b is %d\n", sum(a, b));
   return 0;
}

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

Here is its output −

Sum of a and b is 30

In the above code, the function sum is called before its definition. Since the program contains the prototype of the sum function, it executes without any errors.

What Happens When the Function Prototype is Missing?

When the function prototype is missing in C, the compiler gives the following two errors −

  • Implicit Declaration Warning − This warning indicates that the compiler cannot find a function with the given name.
  • Incorrect Return Type Warning − You will get this warning when a function’s return type is not explicitly specified. By default, the compiler assumes the return type to be int.

Example

This program shows the effect when the function prototype is missing in a C program −

#include <stdio.h>


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

int main() {
   int a = 10, b = 20; 
   printf("Sum of a and b is %d\n", sum(a, b));
   return 0;
}

When you run this code, it will flash the following error −

ERROR!
/tmp/uG7dM1iUzv/main.c: In function 'main':
/tmp/uG7dM1iUzv/main.c:6:36: error: implicit declaration of function 'sum' [-Wimplicit-function-declaration]
    6 |    printf("Sum of a and b is %d\n",sum(a, b));

In the above code, the function sum is called before it is defined and we don't have a function prototype of sum at the beginning. Hence, this code results in an implicit declaration error.

Benefits of Using Function Prototypes

Following are the benefits of using function prototypes −

  • Function definition before definition − It allows a function to be called before its definition, but make sure the function prototypes are declared at the beginning. In complex programs, it is common to declare the function prototype at the beginning or in the header of the code, which enables function calls to occur before the function’s actual implementation.
  • Type checking − A function prototype allows the compiler to check whether the correct number and types of arguments are passed to the function.
  • Code clarity − Declaring the function prototypes makes the programmers aware of the function’s purpose and expected parameters, which helps in understanding the code structure.

In this chapter, we covered in detail the importance of using function prototypes in C programming.

Advertisements