C++ Function Declaration
In this tutorial, you will learn the concept of function declaration in C++, its syntax, usage, and examples.
What is a Function Declaration?
A function declaration, also known as a function prototype, informs the compiler about the name, return type, and parameters of a function.
A function declaration is typically written before the main() function or in a header file to allow reuse in multiple source files.
Syntax of a Function Declaration
return_type function_name(parameter_list);
Here’s a breakdown of the syntax:
- return_type: The data type of the value the function returns (e.g.,
int
,void
). - function_name: The unique identifier for the function.
- parameter_list: A comma-separated list of parameters, each with a type and name.
Example of Function Declaration
Below is an example of declaring a function:
int add(int a, int b);
This declaration states that the function add
takes two int
parameters and returns an int
.
1. Function with void
Return Type
void printMessage();
Explanation: The function printMessage
takes no parameters and does not return a value.
2. Function with a double
Return Type
double calculateArea(double radius);
Explanation: The function calculateArea
takes a single double
parameter (radius) and returns a double
value representing the area.
3. Function with Multiple Parameters
float computeAverage(int num1, int num2, int num3);
Explanation: The function computeAverage
accepts three int
parameters and returns a float
value.
4. Function Returning a char
char getGrade(int score);
Explanation: The function getGrade
takes an int
parameter (score) and returns a char
value representing the grade.
5. Function with Default Parameter Values
int multiply(int a, int b = 1);
Explanation: The function multiply
has two parameters. The second parameter, b
, has a default value of 1
. If only one argument is passed, b
will default to 1
.
6. Function with Array Parameter
void printArray(int arr[], int size);
Explanation: The function printArray
takes two parameters: an array of integers arr
and its size size
. It does not return a value.
7. Function Returning a Pointer
int* findMax(int* arr, int size);
Explanation: The function findMax
takes a pointer to an integer array arr
and its size size
. It returns a pointer to the largest integer in the array.
8. Function with Constant Parameters
double calculateDistance(const double x1, const double y1, const double x2, const double y2);
Explanation: The function calculateDistance
takes four constant double
parameters representing two points in a plane. It returns the distance between them.
Complete Example with Function Definition
The following code demonstrates both the declaration and definition of a function:
Program – main.cpp
#include <iostream>
using namespace std;
// Function declaration
int add(int a, int b);
int main() {
int num1 = 10, num2 = 20;
cout << "Sum: " << add(num1, num2) << endl;
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Before the main() function, we have made the function declaration for add(). And then we used the add() function inside the main(). We then defined the add() function after the main function.
Detailed Explanation of the Code
- Function Declaration
-
</>Copy
This is the function declaration (also called the function prototype). It tells the compiler the function name (int add(int a, int b);
add
), return type (int
), and parameters (int a
,int b
). The actual implementation of the function comes later in the code. - Main Function
-
</>Copy
Theint main() { int num1 = 10, num2 = 20; cout << "Sum: " << add(num1, num2) << endl; return 0; }
main
function is the entry point of the program. It performs the following steps:
1. Two integer variables,num1
andnum2
, are initialized with values10
and20
.
2. Theadd
function is called withnum1
andnum2
as arguments.
3. The result of theadd
function is printed to the console usingcout
.
4. The program returns0
, indicating successful execution. - Function Definition
-
</>Copy
Theint add(int a, int b) { return a + b; }
add
function is defined here. It performs the actual computation:
1. The function takes two integer parameters,a
andb
.
2. It computes the sum ofa
andb
using the+
operator.
3. The result is returned to the calling function (in this case,main
).
Key Points about Function Declaration
- The function declaration ends with a semicolon (
;
). - If a function is declared but not defined, the program will result in a linker error.
- Function declarations are often placed in header files to enable modular programming.
Function Declaration vs. Definition
The following table gives the differences between function declaration and function definition with respect to purpose, syntax, and location in the program.
Aspect | Function Declaration | Function Definition |
---|---|---|
Purpose | Introduces the function to the compiler. | Provides the actual implementation of the function. |
Syntax | Ends with a semicolon (; ). | Enclosed within curly braces ({} ). |
Location | Before the main() function or in a header file. | In the source file. |
Best Practices for Function Declaration
- Use meaningful names for functions and parameters for better readability.
- Declare functions in header files and define them in source files for cleaner code organization.
- Ensure the function declaration matches the function definition exactly in terms of return type and parameters.