Skip to content
Exercise · Functions · lesson 5 of 11

Exercise: swap two values

Write a function void swap_values(int& a, int& b): after the call, a holds the old value of b, and b the old value of a.

  • int x = 1; int y = 2; swap_values(x, y); → x == 2, y == 1
  • int a = -7; int b = 40; swap_values(a, b); → a == 40, b == -7

Constraints: a and b are any int values, including equal ones.

swap.cpp — cpp
cpp · 3 lines
Ctrl+Enter run · Esc leave editor