C++ Academy · Lesson

const Variables and Parameters

Protect data from change.

Lesson 1 of 413 steps

const Variables and Parameters is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “const Variables and Parameters” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “const Variables and Parameters”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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

  1. const Variables and Parameters
  2. const Member Functions
  3. constexpr and Compile-Time
  4. consteval and constinit
← Back to C++ Academy