Function Declaration vs Function Expression

Kartikey KatyalKartikey Katyal
2 min read

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:

  1. Declares function before use – Useful in compiled languages like C.

  2. Specifies function signature – Includes name, return type, and parameters.

  3. 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

FeatureFunction DeclarationFunction Expression
Hoisting✅ Yes (can be called before declaration)❌ No (must be declared first)
Syntaxfunction myFunc() {}const myFunc = function() {};
NamingRequiredOptional (anonymous or named)
Use CasesGlobal functions, reusable codeDynamic functions, callbacks, event handlers
Stack Trace NameClearly named in errorsMay 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.

5
Subscribe to my newsletter

Read articles from Kartikey Katyal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kartikey Katyal
Kartikey Katyal