const Variables and Parameters
Protect data from change.
const Variables and Parameters is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Declaring const Variables
A const variable cannot be modified after initialization. The compiler enforces this, catching accidental writes.
#include <iostream>
int main() {
const int maxUsers = 100;
// maxUsers = 200; // ERROR
std::cout << maxUsers << "\n";
}const Function Parameters
Marking a by-value parameter const prevents the function body from altering its local copy — a documentation and safety aid.
#include <iostream>
int doubled(const int n) {
// n = 0; // ERROR
return n * 2;
}
int main() { std::cout << doubled(21) << "\n"; }const Reference Parameters
Passing by const& avoids copying large objects while promising not to modify them — the standard way to pass strings and containers.
#include <iostream>
#include <string>
void printLen(const std::string& s) { // no copy, no change
std::cout << s.size() << "\n";
}
int main() { printLen("hello"); }Pointer to const
const T* means you cannot change the pointed-to value through this pointer, though the pointer itself may be reseated.
#include <iostream>
int main() {
int a = 1, b = 2;
const int* p = &a;
// *p = 5; // ERROR: value is const
p = &b; // OK: pointer can move
std::cout << *p << "\n";
}const Pointer
T* const is a constant pointer: you cannot reseat it, but you may modify the value it points to.
#include <iostream>
int main() {
int a = 1;
int* const p = &a;
*p = 9; // OK: value changeable
// p = nullptr; // ERROR: pointer is const
std::cout << *p << "\n";
}Reading Declarations Right-to-Left
Read const declarations from right to left: const int* const is a "const pointer to const int" — neither value nor pointer can change.
Why const Correctness?
Using const everywhere it applies documents intent, prevents bugs, and lets the compiler optimize. This discipline is called const correctness.
const Enables const Callers
A function taking const& can accept both const and non-const arguments, plus temporaries. A non-const reference cannot bind to a temporary.
const Local Helps Reasoning
Declaring locals const when they should not change makes code easier to read and prevents accidental reassignment.
#include <iostream>
int main() {
const double rate = 0.2;
const double total = 100 * (1 + rate);
std::cout << total << "\n";
}Top-Level const on Values
For by-value parameters, top-level const does not affect the caller — it only constrains the function body and is often omitted in declarations.
Add const Early
It is far easier to start const-correct than to retrofit it. Apply const to parameters, references, and locals as a default habit.
Quick Check
Test your understanding of const.
Recap
You learned to use const on variables, parameters, references, and pointers, and the difference between pointer to const and const pointer. const correctness documents intent, prevents bugs, and enables binding to temporaries and optimization.
Frequently asked questions
Is the “const Variables and Parameters” lesson free?
Yes — the full text of “const Variables and Parameters” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “const Variables and Parameters”?
Protect data from change. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “const Variables and Parameters” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- const Variables and Parameters
- const Member Functions
- constexpr and Compile-Time
- consteval and constinit