CS Electrical And Electronics
@cselectricalandelectronics

What is Default Arguments with example:

All QuestionsCategory: Cpp LanguageWhat is Default Arguments with example:
Anonymous asked 3 years ago
1 Answers
Anonymous answered 3 years ago

C++ functions can be assigned with default values. In absence of real arguments, default values will be considered Default values must be assigned from right to left, i.e. if one intermediate argument has a default value all subsequent elements also will have default values.
 
Example Code
 

int sum(int, int, int = 30);

int main() {

  int a = 10, b = 20, c = 18;
  int r1, r2;

  r1 = sum(a, b, c);
  r2 = sum(a, b);
  // print r1,r2

  return 0;
}

int sum(int x, int y, int z) { return x + y + z; }


Output:


48
60