Open In App

Function Declaration vs. Function Definition

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Function Declaration introduces the name, return type, and parameters of a function to the compiler, while Function Definition provides the actual implementation or body of the function. Declarations enable calling the function elsewhere in the code, while definitions provide the code to be executed when the function is called. In this article, we will learn the difference between ‘function declaration’ and ‘function Definition.

Function Declaration:

A function declaration in programming introduces the name, return type, and parameters of a function to the compiler. It provides information about the function’s interface without including its implementation. Function declarations enable other parts of the program to call the function without knowing its internal details.

Syntax:

void function_name(parameters);

Function Definition:

A function definition in programming provides the actual implementation or body of a function. It contains the code that defines what the function does, including the statements to be executed when the function is called. Function definitions also specify the return type, parameter types, and name of the function, matching the declaration to enable proper execution.

Syntax:

return_type function_name(parameters) {
// Function body (implementation)
// Statements defining what the function does
// Usually ends with a return statement
}

Difference between Function Declaration and Function Definition:

Function Declaration Function Definition
Declares the existence of a function, providing its name and parameters Provides the actual implementation of the function, defining what the function does
Function Declaration is used to let the compiler know about the existence of such a function so that we don’t encounter any Reference Errors. Function Definition is used to provide the actual implementation of the code which will execute every time the function is called.

Function Declaration does not include the body of the function.

Function Definition includes the body of the function.

Syntax: return_type function_name(paramA, paramB); Syntax: return_type function_name(paramA, paramB) { Implementation details }

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads