Skip to content
Learn Netverks

Lesson

Step 10/36 28% through track

functions-cpp

Functions

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Functions: the syntax, patterns, and safety habits you need before advancing in C++.

Teams still ship Functions in C++ codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Functions in contexts like: Game engines, trading systems, desktop apps, and performance-critical libraries.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Functions group reusable logic. C++ supports overloading (same name, different parameters), default arguments, and inline hints—features beyond C.

Overloading and defaults

int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }

void greet(const std::string& name = "world") {
    std::cout << "Hello, " << name << "\n";
}

Return types

Return by value for small types; return const T& only when referring to existing storage with stable lifetime—never return references to locals.

Important interview questions and answers

  1. Q: Overload resolution?
    A: Compiler picks the best match by parameter types at compile time—no runtime dispatch unless virtual.
  2. Q: Default argument rules?
    A: Defaults must be trailing; specify in declaration (often header) consistently.

Self-check

  1. Can two functions share a name if parameters differ?
  2. Why not return a reference to a local variable?

Pitfall: Never return references or pointers to local variables—undefined behavior when the stack frame ends.

Interview prep

Function overloading?

Same function name with different parameter lists—resolved at compile time.

Return reference to local?

Undefined behavior—the local is destroyed when the function returns.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Overload resolution?
  • Default args?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump