Skip to content
Justice Tech School

Default Arguments

Give parameters default values. They must be the last ones.

Example
#include <iostream>

int power(int base, int exp = 2) { int r = 1; while (exp--) r *= base; return r; }

int main() {
  std::cout << power(2) << " " << power(2, 5) << "\n";
  return 0;
}

Result

4 32

Exercise

Default value.

int p(int a, int b  2)

Quiz Test

Where do defaults go?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.