June 2025
Intermediate to advanced
1093 pages
33h 24m
English
In the previous section, I defined a function that adds two values:
int add(int n, int m) { return n + m; }
If you want to write a function that adds more than just two values, you can now overload it:
int add(int n, int m, int o) { return n+m+o; }int add(int n, int m, int o, int p) { return n+m+o+p; }int add(int n, int m, int o, int p, int q) { return n+m+o+p+q; }
Here you have various versions of add, which allow you to sum up to five numbers at once. They all share the same name add. But wouldn't it be convenient to write just one function? This becomes even more practical if you need more than five variants.
You can achieve this with the help of default arguments. Simply write default values for the parameters, ...
Read now
Unlock full access