Function Declaration vs Function Expression
What is Function Declaration ?
A Function Declaration is a way of defining a function in programming . It tells the compiler function name , return type and parameter before it is used It allows the function to be called before its actual definition in the code
Function declaration Syntax in C/C++/JS
returnType functionName(parameterType parameterName, ...);
Key Points:
Declares function before use – Useful in compiled languages like C.
Specifies function signature – Includes name, return type, and parameters.
Allows separation of declaration and definition – Common in header files for C/C++.
What is Function Expression ?
A function expression is a way of defining a function inside an expression and assigning it to a variable. Unlike function declarations, function expressions do not get hoisted, meaning they cannot be called before they are defined.
const variableName = function(parameters) {
// Function body
return something;
};
Key Differences Between Function Declarations and Expressions
Feature | Function Declaration | Function Expression |
Hoisting | ✅ Yes (can be called before declaration) | ❌ No (must be declared first) |
Syntax | function myFunc() {} | const myFunc = function() {}; |
Naming | Required | Optional (anonymous or named) |
Use Cases | Global functions, reusable code | Dynamic functions, callbacks, event handlers |
Stack Trace Name | Clearly named in errors | May be anonymous |
When to Use Each?
Use Function Declarations when:
✅ You need to call the function before it's defined.
✅ The function is meant to be reusable throughout the script.
✅ Readability and stack trace clarity are important.
Use Function Expressions when:
✅ You need to define functions dynamically inside expressions.
✅ You’re working with callbacks or immediately invoked function expressions (IIFE).
✅ You want to control when and how a function is executed.