Functions and overloading
A function is a named block of code that receives data, does a job and returns a result. Splitting a program into small functions makes it easier to read, test and reuse: each function should do one thing and its name should say which. You have used a function since day one, main; now you will write your own, pass parameters and give the same name several versions through overloading.
Declaring and defining
The general form is returnType name(parameters) { body }. If the function returns nothing, the type is void. A value is returned with return, which also ends the function. In C++ the compiler reads top to bottom, so a function must be declared before it is used. You can write its prototype first (the signature ending in a semicolon) and define the body later; this idea will be the base of header files in week 8.
#include <iostream>
#include <string>
// prototype: declared before use
int square(int x);
bool isEven(int n) {
return n % 2 == 0;
}
void printGreeting(const std::string& name) {
std::cout << "Hello, " << name << "!\n";
}
int main() {
printGreeting("Ada");
std::cout << "square(7) = " << square(7) << '\n';
std::cout << std::boolalpha << "isEven(10) = " << isEven(10) << '\n';
return 0;
}
// definition after main
int square(int x) {
return x * x;
}Value parameters and default arguments
By default parameters are passed by value: the function receives a copy, so changing it inside does not alter the original variable. This is safe, but copying large objects is costly; in the next lesson you will see how to avoid it with references. A parameter can have a default value, used when the caller does not give one: void greet(std::string name, std::string greeting = "Hello"). Defaults must come at the end of the parameter list and are written only in the declaration, not repeated in the definition.
Function overloading
C++ allows several functions with the same name as long as they differ in the number or type of their parameters. The compiler picks the right version from the arguments of the call. It lets you offer one operation on different types without inventing names like areaCircle or areaRect. What is not enough to overload is changing only the return type: the compiler decides from the arguments, not from what you expect to receive. If a call matches several versions ambiguously, you get an error that you solve with a cast or another signature.
#include <iostream>
#include <string>
constexpr double kPi = 3.14159265358979;
double area(double radius) { // circle
return kPi * radius * radius;
}
double area(double width, double height) { // rectangle
return width * height;
}
void show(int value) {
std::cout << "int: " << value << '\n';
}
void show(const std::string& value) {
std::cout << "string: " << value << '\n';
}
void greet(const std::string& name, const std::string& greeting = "Hello") {
std::cout << greeting << ", " << name << "!\n";
}
int main() {
std::cout << area(2.0) << '\n'; // circle
std::cout << area(3.0, 4.0) << '\n'; // rectangle: 12
show(42);
show(std::string{"forty-two"});
greet("Linus");
greet("Linus", "Good morning");
return 0;
}Common mistakes: forgetting return in a function that must return a value (undefined behavior; -Wall warns about it), calling a function before declaring it, and expecting a function to modify the argument when it was passed by value. Remember also that variables declared inside a function are local: they exist only while the function runs and nobody outside can see them.
✎ My notes
Saved only in this browser. Sign in to keep it in your account.0/5000 characters