What is the function Prototype in C
In the C programming language, a function prototype is an assertion of a function that details its function identity, return type, and the quantity and categories of its arguments. It serves as an agreement between the description of the function and the individuals who invoke it, outlining the desired inputs and outcomes.
In C, a function is a code that performs an operation and provides functionality. The fundamental C language element is the prototype. It informs the interpreter of the function's return type, all of the variables it embraces, and the data type of those parameters to prevent problems and warnings.
Syntax:
Below is an example of a C function's syntax:
return_type function_name(datatype argument1, datatype argument2, datatype argument3………………)
{
// body of the function
}
Example:
int Addition (float a, float b)
{
return a + b;
}
In this function prototype, it's simple to see:
- Code return type: decimals or float
- There are two parameters.
- Argument data type: float
Function prototypes are used by the interpreter to verify the validity of function calls and to perform type inspection, which prevents the passing of unsupported data formats to functions. Function prototypes are often included before the primary function is defined in header files or before the start of the code. They also aid in catching potential mistakes at the beginning of compilation instead of during execution.
Using Function Prototypes in C is essential. Because it informs the interpreter regarding the function's return type, maximum number of variables, and data types associated with these variables, the function prototype in the C language is crucial. The program's compiler uses this data to verify the code for type consistency and guarantee that the given function is called with the appropriate number and kinds of parameters.
Runtime faults are less likely because they operate in preliminary designs, allowing the compilation process to inspect for possible bugs before the code execution.
Furthermore, function prototypes enhance the flexible nature and reuse of software written in C by enabling methods to be defined and utilized in different folders or chunks of code.
Below given is a function prototype implementation in C:
Example Code 1:
#include <stdio.h>
int square (int n);
int main ()
{
int n;
printf ("Enter a number: ");
scanf ("%d", &n);
printf ("the square of the number given input from user is”, square(n));
return 0;
}
int square (int n1)
{
return n1 * n1;
}
Output:
![What is the function Prototype in C](https://static.tutorialandexample.com/c/what-is-the-function-prototype-in-c1.png)
To deliver data regarding the return type and arguments of the square () method, the function prototype int square (int n); is defined at the first line of the program. The square of n is returned by the square () function, which accepts an integer n as an argument.
The function's main call calls the square () function, which then displays the outcome.
Example Code 2:
#include <stdio.h>
#include<math.h>
#include<string.h>
int multiplication (int FirstNumber,int SecondNumber); // function prototype for multiply function
int main ()
{
int FirstNumber;// first number
int SecondNumber;// second number
printf ("Fill out the first digit :");
scanf ( "%d",&FirstNumber) ; // taking first number as input from user
printf(" Fill out the subsequent digit :" );
scanf( "%d",&SecondNumber) ; // taking second number as input from user
int result = multiplication (FirstNumber,SecondNumber);// invoking multiplication function
printf(" multiplying two numbers gives %d",result); // displaying the product of two numbers
return 0 ;
}
// Function definition, in this case, follows the main program.
int multiplication (int FirstNumber,int SecondNumber)// function definition
{
return FirstNumber*SecondNumber;// gives the multiplication of the two numbers
Output:
![What is the function Prototype in C](https://static.tutorialandexample.com/c/what-is-the-function-prototype-in-c2.png)
The program below multiplies two numbers. The multiplication function was set up first in this case and subsequently in the program using a function prototype.
Function Definition vs. Function Prototype in C: What's the Difference?
A function definition and prototype have two distinct functions in C:
Function Prototype:
A function prototype informs the software developer of the function's name, variables, and return type. It declares the interpreter of the function's name and is defined at the initial stage of the program before the primary function is defined. As a result, whenever the function is called a while later in the source code, it will know how to invoke it accurately.
Syntax:
int addition (int x, int y);
Function Definition:
On the other hand, the entire structure of a function, involving the return type, identity, variables, and instructions that constitute the function's body, is provided by a function definition. The main function of the program, or all function prototypes, tends to appear before the function declaration.
Syntax:
int addition (int x, int y)
{
return x+y;
}