[C++] Function

두꺼비·2022년 1월 11일
2
post-custom-banner

C++ Default Parameters

void myFunction(string country = "Norway") {
  cout << country << "\n";
}

int main() {
  myFunction("Sweden");
  myFunction("India");
  myFunction();
  myFunction("USA");
  return 0;
}

Sweden
India
Norway
USA


C++ Functions - Pass By Reference

#include <iostream>
using namespace std;

void swapNums(int &x, int &y) {
  int z = x;
  x = y;
  y = z;
}

int main() {
  int firstNum = 10;
  int secondNum = 20;

  cout << "Before swap: " << "\n";
  cout << firstNum << secondNum << "\n";

  swapNums(firstNum, secondNum);

  cout << "After swap: " << "\n";
  cout << firstNum << secondNum << "\n";

  return 0;
}

Before swap:
1020
After swap:
2010






참고 : W3schools

profile
두꺼비는 두껍다
post-custom-banner

0개의 댓글