C++ Academy · Lesson

Passing Spans to Functions

Write flexible APIs.

Lesson 3 of 413 steps

Passing Spans to Functions is a free C++ Academy lesson on CoddyKit. This is lesson 3 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.

One Function, Many Containers

A function taking std::span accepts arrays, vectors, and raw buffers alike, no templates or overloads needed.

#include <iostream>
#include <span>
#include <vector>
#include <array>
int sum(std::span<const int> s) {
    int t = 0; for (int x : s) t += x; return t;
}
int main() {
    std::vector<int> v = {1, 2, 3};
    std::array<int, 2> a = {4, 5};
    std::cout << sum(v) << " " << sum(a) << "\n";
}

No Size Parameter Needed

Old C-style APIs pass a pointer plus a separate size. A span bundles both, eliminating mismatched-length bugs.

#include <iostream>
#include <span>
void printAll(std::span<const int> s) {
    for (int x : s) std::cout << x << " ";
    std::cout << "\n";
}
int main() {
    int buf[] = {7, 8, 9};
    printAll(buf); // size inferred
}

const span for Read-Only

Use std::span<const T> to promise the function will not modify the caller's data.

#include <iostream>
#include <span>
int maxOf(std::span<const int> s) {
    int m = s[0];
    for (int x : s) if (x > m) m = x;
    return m;
}
int main() {
    int data[] = {3, 9, 4};
    std::cout << maxOf(data) << "\n"; // 9
}

Mutable span to Edit In Place

A non-const span lets a function modify the caller's elements directly.

#include <iostream>
#include <span>
#include <vector>
void doubleAll(std::span<int> s) {
    for (int& x : s) x *= 2;
}
int main() {
    std::vector<int> v = {1, 2, 3};
    doubleAll(v);
    std::cout << v[2] << "\n"; // 6
}

Avoids Template Bloat

Without span you might template on container type, generating many instantiations. A span gives one concrete signature that just works.

Avoids Copies

Passing a span never copies the elements, only a pointer and length travel into the function.

#include <iostream>
#include <span>
#include <vector>
size_t count(std::span<const int> s) { return s.size(); }
int main() {
    std::vector<int> big(1000, 0);
    std::cout << count(big) << "\n"; // no copy of 1000 ints
}

Empty Spans Are Fine

A function should handle an empty span gracefully; size() is zero and the range-based loop simply runs zero times.

#include <iostream>
#include <span>
int sum(std::span<const int> s) { int t = 0; for (int x : s) t += x; return t; }
int main() {
    std::span<const int> empty;
    std::cout << sum(empty) << "\n"; // 0
}

Pass by Value

Spans are tiny, so pass them by value, not by reference. Copying a pointer and length is essentially free.

Document Const Intent

Choosing span<const T> versus span<T> tells callers at a glance whether their data may be modified.

Caller Keeps Ownership

The caller still owns the container; the span only borrows it for the duration of the call. The function must not store the span beyond the call.

A Modern API Style

Accepting a span is the idiomatic modern way to write functions over contiguous data, flexible, safe, and copy-free.

Quick Check

Check your understanding of passing spans.

Recap

You learned to pass spans to functions:

  • One signature accepts arrays, vectors, and buffers.
  • span<const T> for read-only; span<T> to edit in place.
  • Pass by value, copies nothing.
  • The caller keeps ownership; do not store the span past the call.
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 “Passing Spans to Functions” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Passing Spans to Functions”, 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 “Passing Spans to Functions”?

Write flexible APIs. 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 3 of 4.

How long does the “Passing Spans to Functions” 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. Fixed-Size std::array
  2. std::span Basics
  3. Passing Spans to Functions
  4. Bounds and Subspans
← Back to C++ Academy