Skip to content
Exercise · Functions · lesson 6 of 13

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.

Do not add extra #include lines unless the task says so explicitly.

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

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way