
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is the purpose of a function prototype in C/C++?
Here we will see what are the purpose of using function prototypes in C or C++. The function prototypes are used to tell the compiler about the number of arguments and about the required datatypes of a function parameter, it also tells about the return type of the function. By this information, the compiler cross-checks the function signatures before calling it. If the function prototypes are not mentioned, then the program may be compiled with some warnings, and sometimes generate some strange output.
If some function is called somewhere, but its body is not defined yet, that is defined after the current line, then it may generate problems. The compiler does not find what is the function and what is its signature. In that case, we need to function prototypes. If the function is defined before then we do not need prototypes.
Example Code
#include<stdio.h> main() { function(50); } void function(int x) { printf("The value of x is: %d", x); }
Output
The value of x is: 50
This shows the output, but it is showing some warning like below:
[Warning] conflicting types for 'function' [Note] previous implicit declaration of 'function' was here
Now using function prototypes, it is executing without any problem.
Example Code
#include<stdio.h> void function(int); //prototype main() { function(50); } void function(int x) { printf("The value of x is: %d", x); }
Output
The value of x is: 50
- Related Articles
- What is function prototype in C language
- Importance of function prototype in C
- What is the purpose of ‘is’ operator in C#?
- What is the purpose of ‘as’ operator in C#?
- What is the purpose of the StringBuilder class in C#?
- What is the purpose of a self-executing function in JavaScript?
- What is the purpose of MySQL TRIM() function?
- What is the purpose of an access specifier in C#?
- What is the purpose of Program.cs file in C# ASP.NET Core project?
- What is the Address of a function in C or C++?\n
- Purpose of Unions in C/ C++
- What is a reentrant function in C/C++?
- What is the purpose of using MySQL CHAR_LENGTH() function? Which function is the synonym of it?
- What is a free function in C++?
- What is a function specifier in C?
