Function Declaration vs Function Definition

Mayowa Obisesan
2 min readDec 12, 2023

Generated by bing Images

In C++ and other low-level languages such as Solidity, there is a commonly used term: declaring a function and defining a function. What’s the difference?

#include <iostream>

void Log(const char* message) {
std::cout << message << std::endl;
}

int Multiply(int a, int b) {
Log("A multiply function is being called");
return a * b;
}

int main() {
std::cout << Multiply(2, 5) << std::endl;
std::cin.get();
}

Take a look at this C++ code named math.cpp which defines two functions called Multiply and Log. I used the term define here because the functions have its type, its name, its parameters and most importantly the functions contain a body. It is because the function has a body that it is referred to as a function definition. So, if a function with a body is a function definition, then that means that… exactly. It means that a function without a body is a function declaration. That’s it. It’s that simple.

Take a look at this code below, you’ll notice that the Log function no longer have a body, that means there is no curly braces and no code logic within a curly brace. The Log function in the below code is now declared. It is a function declaration.

#include <iostream>

void Log(const char* message); // Function declaration

int Multiply(int a, int b) {
Log("A multiply function is being called");
return a * b;
}

int main() {
std::cout << Multiply(2, 5) << std::endl;
std::cin.get();
}

This subtle difference of a function having a body and not having a body is the difference between a function declaration and a function definition.

I hope you have learnt something from this article.

You can check out my other articles about C++, Rust, programming and technical concepts.

Thanks for reading. 🙂

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Mayowa Obisesan
Mayowa Obisesan

Written by Mayowa Obisesan

Entrepreneur | Computer Scientist

No responses yet

What are your thoughts?