C++ Beginner · Lesson 08 of 22
Functions: parameters, return types and overloading
Declaring and calling functions, default arguments, and overloads by parameter type.
Estimated time · 45–75 min
Learning goals
What you will understand
- Explain functions: parameters, return types and overloading in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
A function gives a piece of logic a name, typed inputs and an output. Pass small values (int, double) by value and larger objects by const reference (const std::string&). void means no return value.
Overloading allows several functions with the same name if their parameter types differ. The compiler picks the best match at compile time. Default arguments let callers omit trailing parameters.
Guided lesson
Work through these steps
- 1.
Read the functions: parameters, return types and overloading example and identify what each line owns or changes.
- 2.
Create a small source file, compile with -std=c++17 and warnings enabled, and run it.
- 3.
Change one input, predict the result before running, then compare the output.
- 4.
Record one compiler or runtime error and explain the correction in your own words.
Worked example
Read it before you run it
double clamp(double value, double low, double high) {
// Return type double; three double inputs passed by value.
if (value < low) return low;
// Below the range: return the lower limit.
if (value > high) return high;
// Above the range: return the upper limit.
return value;
// Otherwise the value is already valid.
}
// End of clamp.
void print(int ticks) { std::cout << ticks << " ticks\n"; }
// One overload of print, chosen when the argument is an int.
void print(double rad) { std::cout << rad << " rad\n"; }
// A second overload, chosen when the argument is a double.
double move_by(double delta, double speed = 0.5);
// Declaration with a default: callers may omit speed and get 0.5.
print(2048);
// Calls the int overload.
print(1.57);
// Calls the double overload.What the example is doing
- 1.Read declarations from right to left: identify each value's type, name, and initial value.
- 2.Follow the data through each expression before considering syntax shortcuts.
- 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.
Watch it run · Values in memory
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Functions: parameters, return types and overloading. Expected result: 10 int double
Practice code
Now type it yourself
Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice
#include <iostream>
// TODO: write clamp(value, low, high): return low if below, high if above, otherwise value
// TODO: overload describe() for int (returns "int") and for double (returns "double")
int main() {
std::cout << clamp(12.0, 0.0, 10.0) << '\n';
std::cout << describe(3) << '\n';
std::cout << describe(2.5) << '\n';
return 0;
}Expected output
10
int
doubleStuck? Show the full solution (try it yourself first)
#include <iostream>
double clamp(double value, double low, double high) {
if (value < low) return low;
if (value > high) return high;
return value;
}
const char* describe(int) { return "int"; }
const char* describe(double) { return "double"; }
int main() {
std::cout << clamp(12.0, 0.0, 10.0) << '\n';
std::cout << describe(3) << '\n';
std::cout << describe(2.5) << '\n';
return 0;
}Expected result
How you know it worked
- After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
- Changing the stated input produces the result you predicted.
Practical exercise
Apply the lesson
Write clamp_velocity(requested, limit) that rejects a negative limit. Overload describe() for int and double, add a default argument, then test values below, inside and above the range.
Before you continue
- The program builds with -std=c++17 and no warnings
- You can explain the key rule without reading the example
- The normal and edge cases both have recorded results
If something goes wrong
Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.
Print the inputs and intermediate values, then check conversions, units, and collection order.